]> git.proxmox.com Git - libgit2.git/blob - examples/blame.c
Merge pull request #2066 from libgit2/rb/builtin-diff-drivers
[libgit2.git] / examples / blame.c
1 /*
2 * libgit2 "blame" example - shows how to use the blame API
3 *
4 * Written by the libgit2 contributors
5 *
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
9 *
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
13 */
14
15 #include "common.h"
16
17 #ifdef _MSC_VER
18 #define snprintf sprintf_s
19 #define strcasecmp strcmpi
20 #endif
21
22 /**
23 * This example demonstrates how to invoke the libgit2 blame API to roughly
24 * simulate the output of `git blame` and a few of its command line arguments.
25 */
26
27 struct opts {
28 char *path;
29 char *commitspec;
30 int C;
31 int M;
32 int start_line;
33 int end_line;
34 };
35 static void parse_opts(struct opts *o, int argc, char *argv[]);
36
37 int main(int argc, char *argv[])
38 {
39 int i, line, break_on_null_hunk;
40 char spec[1024] = {0};
41 struct opts o = {0};
42 const char *rawdata;
43 git_repository *repo = NULL;
44 git_revspec revspec = {0};
45 git_blame_options blameopts = GIT_BLAME_OPTIONS_INIT;
46 git_blame *blame = NULL;
47 git_blob *blob;
48 git_object *obj;
49
50 git_threads_init();
51
52 parse_opts(&o, argc, argv);
53 if (o.M) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES;
54 if (o.C) blameopts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES;
55
56 /** Open the repository. */
57 check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository", NULL);
58
59 /**
60 * The commit range comes in "commitish" form. Use the rev-parse API to
61 * nail down the end points.
62 */
63 if (o.commitspec) {
64 check_lg2(git_revparse(&revspec, repo, o.commitspec), "Couldn't parse commit spec", NULL);
65 if (revspec.flags & GIT_REVPARSE_SINGLE) {
66 git_oid_cpy(&blameopts.newest_commit, git_object_id(revspec.from));
67 git_object_free(revspec.from);
68 } else {
69 git_oid_cpy(&blameopts.oldest_commit, git_object_id(revspec.from));
70 git_oid_cpy(&blameopts.newest_commit, git_object_id(revspec.to));
71 git_object_free(revspec.from);
72 git_object_free(revspec.to);
73 }
74 }
75
76 /** Run the blame. */
77 check_lg2(git_blame_file(&blame, repo, o.path, &blameopts), "Blame error", NULL);
78
79 /**
80 * Get the raw data inside the blob for output. We use the
81 * `commitish:path/to/file.txt` format to find it.
82 */
83 if (git_oid_iszero(&blameopts.newest_commit))
84 strcpy(spec, "HEAD");
85 else
86 git_oid_tostr(spec, sizeof(spec), &blameopts.newest_commit);
87 strcat(spec, ":");
88 strcat(spec, o.path);
89
90 check_lg2(git_revparse_single(&obj, repo, spec), "Object lookup error", NULL);
91 check_lg2(git_blob_lookup(&blob, repo, git_object_id(obj)), "Blob lookup error", NULL);
92 git_object_free(obj);
93
94 rawdata = git_blob_rawcontent(blob);
95
96 /** Produce the output. */
97 line = 1;
98 i = 0;
99 break_on_null_hunk = 0;
100 while (i < git_blob_rawsize(blob)) {
101 const char *eol = strchr(rawdata+i, '\n');
102 char oid[10] = {0};
103 const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, line);
104
105 if (break_on_null_hunk && !hunk) break;
106
107 if (hunk) {
108 break_on_null_hunk = 1;
109 char sig[128] = {0};
110
111 git_oid_tostr(oid, 10, &hunk->final_commit_id);
112 snprintf(sig, 30, "%s <%s>", hunk->final_signature->name, hunk->final_signature->email);
113
114 printf("%s ( %-30s %3d) %.*s\n",
115 oid,
116 sig,
117 line,
118 (int)(eol-rawdata-i),
119 rawdata+i);
120 }
121
122 i = (int)(eol - rawdata + 1);
123 line++;
124 }
125
126 /** Cleanup. */
127 git_blob_free(blob);
128 git_blame_free(blame);
129 git_repository_free(repo);
130
131 git_threads_shutdown();
132
133 return 0;
134 }
135
136 /** Tell the user how to make this thing work. */
137 static void usage(const char *msg, const char *arg)
138 {
139 if (msg && arg)
140 fprintf(stderr, "%s: %s\n", msg, arg);
141 else if (msg)
142 fprintf(stderr, "%s\n", msg);
143 fprintf(stderr, "usage: blame [options] [<commit range>] <path>\n");
144 fprintf(stderr, "\n");
145 fprintf(stderr, " <commit range> example: `HEAD~10..HEAD`, or `1234abcd`\n");
146 fprintf(stderr, " -L <n,m> process only line range n-m, counting from 1\n");
147 fprintf(stderr, " -M find line moves within and across files\n");
148 fprintf(stderr, " -C find line copies within and across files\n");
149 fprintf(stderr, "\n");
150 exit(1);
151 }
152
153 /** Parse the arguments. */
154 static void parse_opts(struct opts *o, int argc, char *argv[])
155 {
156 int i;
157 char *bare_args[3] = {0};
158
159 if (argc < 2) usage(NULL, NULL);
160
161 for (i=1; i<argc; i++) {
162 char *a = argv[i];
163
164 if (a[0] != '-') {
165 int i=0;
166 while (bare_args[i] && i < 3) ++i;
167 if (i >= 3)
168 usage("Invalid argument set", NULL);
169 bare_args[i] = a;
170 }
171 else if (!strcmp(a, "--"))
172 continue;
173 else if (!strcasecmp(a, "-M"))
174 o->M = 1;
175 else if (!strcasecmp(a, "-C"))
176 o->C = 1;
177 else if (!strcasecmp(a, "-L")) {
178 i++; a = argv[i];
179 if (i >= argc) fatal("Not enough arguments to -L", NULL);
180 check_lg2(sscanf(a, "%d,%d", &o->start_line, &o->end_line)-2, "-L format error", NULL);
181 }
182 else {
183 /* commit range */
184 if (o->commitspec) fatal("Only one commit spec allowed", NULL);
185 o->commitspec = a;
186 }
187 }
188
189 /* Handle the bare arguments */
190 if (!bare_args[0]) usage("Please specify a path", NULL);
191 o->path = bare_args[0];
192 if (bare_args[1]) {
193 /* <commitspec> <path> */
194 o->path = bare_args[1];
195 o->commitspec = bare_args[0];
196 }
197 if (bare_args[2]) {
198 /* <oldcommit> <newcommit> <path> */
199 char spec[128] = {0};
200 o->path = bare_args[2];
201 sprintf(spec, "%s..%s", bare_args[0], bare_args[1]);
202 o->commitspec = spec;
203 }
204 }