git-blame: no rev means start from the working tree file.
Warning: this changes the semantics. This makes "git blame" without any positive rev to start digging from the working tree copy, which is made into a fake commit whose sole parent is the HEAD. It also adds --contents <file> option to pretend as if the working tree copy has the contents of the named file. You can use '-' to make the command read from the standard input. If you want the command to start annotating from the HEAD commit, you need to explicitly give HEAD parameter. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 30, 2007 at 01:11 UTC
1cfe77333f274c9ba9879c2eb61057a790eb050f
3 files changed
+228
-26
builtin-blame.c
+184
-25
@@ -15,9 +15,10 @@
15
#include "revision.h"
16
#include "quote.h"
17
#include "xdiff-interface.h"
18
+#include "cache-tree.h"
19
20
static char blame_usage[] =
20
-"git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [commit] [--] file\n"
21
+"git-blame [-c] [-l] [-t] [-f] [-n] [-p] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [commit] [--] file\n"
22
" -c, --compatibility Use the same output mode as git-annotate (Default: off)\n"
23
" -b Show blank SHA-1 for boundary commits (Default: off)\n"
24
" -l, --long Show long commit SHA1 (Default: off)\n"
@@ -29,6 +30,7 @@ static char blame_usage[] =
30
" -L n,m Process only line range n,m, counting from 1\n"
31
" -M, -C Find line movements within and across files\n"
32
" --incremental Show blame entries as we find them, incrementally\n"
33
+" --contents file Use <file>'s contents as the final image\n"
34
" -S revs-file Use revisions from revs-file instead of calling git-rev-list\n";
35
36
static int longest_file;
@@ -333,9 +335,13 @@ static struct origin *find_origin(struct scoreboard *sb,
335
diff_tree_setup_paths(paths, &diff_opts);
336
if (diff_setup_done(&diff_opts) < 0)
337
die("diff-setup");
336
- diff_tree_sha1(parent->tree->object.sha1,
337
- origin->commit->tree->object.sha1,
338
- "", &diff_opts);
338
+
339
+ if (is_null_sha1(origin->commit->object.sha1))
340
+ do_diff_cache(parent->tree->object.sha1, &diff_opts);
341
+ else
342
+ diff_tree_sha1(parent->tree->object.sha1,
343
+ origin->commit->tree->object.sha1,
344
+ "", &diff_opts);
345
diffcore_std(&diff_opts);
346
347
/* It is either one entry that says "modified", or "created",
@@ -402,9 +408,13 @@ static struct origin *find_rename(struct scoreboard *sb,
408
diff_tree_setup_paths(paths, &diff_opts);
409
if (diff_setup_done(&diff_opts) < 0)
410
die("diff-setup");
405
- diff_tree_sha1(parent->tree->object.sha1,
406
- origin->commit->tree->object.sha1,
407
- "", &diff_opts);
411
+
412
+ if (is_null_sha1(origin->commit->object.sha1))
413
+ do_diff_cache(parent->tree->object.sha1, &diff_opts);
414
+ else
415
+ diff_tree_sha1(parent->tree->object.sha1,
416
+ origin->commit->tree->object.sha1,
417
+ "", &diff_opts);
418
diffcore_std(&diff_opts);
419
420
for (i = 0; i < diff_queued_diff.nr; i++) {
@@ -1047,9 +1057,12 @@ static int find_copy_in_parent(struct scoreboard *sb,
1057
(!porigin || strcmp(target->path, porigin->path)))
1058
diff_opts.find_copies_harder = 1;
1059
1050
- diff_tree_sha1(parent->tree->object.sha1,
1051
- target->commit->tree->object.sha1,
1052
- "", &diff_opts);
1060
+ if (is_null_sha1(target->commit->object.sha1))
1061
+ do_diff_cache(parent->tree->object.sha1, &diff_opts);
1062
+ else
1063
+ diff_tree_sha1(parent->tree->object.sha1,
1064
+ target->commit->tree->object.sha1,
1065
+ "", &diff_opts);
1066
1067
if (!diff_opts.find_copies_harder)
1068
diffcore_std(&diff_opts);
@@ -1336,9 +1349,9 @@ static void get_commit_info(struct commit *commit,
1349
tmp += 2;
1350
endp = strchr(tmp, '\n');
1351
if (!endp)
1339
- goto error_out;
1352
+ endp = tmp + strlen(tmp);
1353
len = endp - tmp;
1341
- if (len >= sizeof(summary_buf))
1354
+ if (len >= sizeof(summary_buf) || len == 0)
1355
goto error_out;
1356
memcpy(summary_buf, tmp, len);
1357
summary_buf[len] = 0;
@@ -1910,6 +1923,137 @@ static int git_blame_config(const char *var, const char *value)
1923
return git_default_config(var, value);
1924
}
1925
1926
+static struct commit *fake_working_tree_commit(const char *path, const char *contents_from)
1927
+{
1928
+ struct commit *commit;
1929
+ struct origin *origin;
1930
+ unsigned char head_sha1[20];
1931
+ char *buf;
1932
+ const char *ident;
1933
+ int fd;
1934
+ time_t now;
1935
+ unsigned long fin_size;
1936
+ int size, len;
1937
+ struct cache_entry *ce;
1938
+ unsigned mode;
1939
+
1940
+ if (get_sha1("HEAD", head_sha1))
1941
+ die("No such ref: HEAD");
1942
+
1943
+ time(&now);
1944
+ commit = xcalloc(1, sizeof(*commit));
1945
+ commit->parents = xcalloc(1, sizeof(*commit->parents));
1946
+ commit->parents->item = lookup_commit_reference(head_sha1);
1947
+ commit->object.parsed = 1;
1948
+ commit->date = now;
1949
+ commit->object.type = OBJ_COMMIT;
1950
+
1951
+ origin = make_origin(commit, path);
1952
+
1953
+ if (!contents_from || strcmp("-", contents_from)) {
1954
+ struct stat st;
1955
+ const char *read_from;
1956
+
1957
+ if (contents_from) {
1958
+ if (stat(contents_from, &st) < 0)
1959
+ die("Cannot stat %s", contents_from);
1960
+ read_from = contents_from;
1961
+ }
1962
+ else {
1963
+ if (lstat(path, &st) < 0)
1964
+ die("Cannot lstat %s", path);
1965
+ read_from = path;
1966
+ }
1967
+ fin_size = st.st_size;
1968
+ buf = xmalloc(fin_size+1);
1969
+ mode = canon_mode(st.st_mode);
1970
+ switch (st.st_mode & S_IFMT) {
1971
+ case S_IFREG:
1972
+ fd = open(read_from, O_RDONLY);
1973
+ if (fd < 0)
1974
+ die("cannot open %s", read_from);
1975
+ if (read_in_full(fd, buf, fin_size) != fin_size)
1976
+ die("cannot read %s", read_from);
1977
+ break;
1978
+ case S_IFLNK:
1979
+ if (readlink(read_from, buf, fin_size+1) != fin_size)
1980
+ die("cannot readlink %s", read_from);
1981
+ break;
1982
+ default:
1983
+ die("unsupported file type %s", read_from);
1984
+ }
1985
+ }
1986
+ else {
1987
+ /* Reading from stdin */
1988
+ contents_from = "standard input";
1989
+ buf = NULL;
1990
+ fin_size = 0;
1991
+ mode = 0;
1992
+ while (1) {
1993
+ ssize_t cnt = 8192;
1994
+ buf = xrealloc(buf, fin_size + cnt);
1995
+ cnt = xread(0, buf + fin_size, cnt);
1996
+ if (cnt < 0)
1997
+ die("read error %s from stdin",
1998
+ strerror(errno));
1999
+ if (!cnt)
2000
+ break;
2001
+ fin_size += cnt;
2002
+ }
2003
+ buf = xrealloc(buf, fin_size + 1);
2004
+ }
2005
+ buf[fin_size] = 0;
2006
+ origin->file.ptr = buf;
2007
+ origin->file.size = fin_size;
2008
+ write_sha1_file(buf, fin_size, blob_type, origin->blob_sha1);
2009
+ commit->util = origin;
2010
+
2011
+ /*
2012
+ * Read the current index, replace the path entry with
2013
+ * origin->blob_sha1 without mucking with its mode or type
2014
+ * bits; we are not going to write this index out -- we just
2015
+ * want to run "diff-index --cached".
2016
+ */
2017
+ discard_cache();
2018
+ read_cache();
2019
+
2020
+ len = strlen(path);
2021
+ if (!mode) {
2022
+ int pos = cache_name_pos(path, len);
2023
+ if (0 <= pos)
2024
+ mode = ntohl(active_cache[pos]->ce_mode);
2025
+ else
2026
+ /* Let's not bother reading from HEAD tree */
2027
+ mode = S_IFREG | 0644;
2028
+ }
2029
+ size = cache_entry_size(len);
2030
+ ce = xcalloc(1, size);
2031
+ hashcpy(ce->sha1, origin->blob_sha1);
2032
+ memcpy(ce->name, path, len);
2033
+ ce->ce_flags = create_ce_flags(len, 0);
2034
+ ce->ce_mode = create_ce_mode(mode);
2035
+ add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
2036
+
2037
+ /*
2038
+ * We are not going to write this out, so this does not matter
2039
+ * right now, but someday we might optimize diff-index --cached
2040
+ * with cache-tree information.
2041
+ */
2042
+ cache_tree_invalidate_path(active_cache_tree, path);
2043
+
2044
+ commit->buffer = xmalloc(400);
2045
+ ident = fmt_ident("Not Committed Yet", "not.committed.yet", NULL, 0);
2046
+ sprintf(commit->buffer,
2047
+ "tree 0000000000000000000000000000000000000000\n"
2048
+ "parent %s\n"
2049
+ "author %s\n"
2050
+ "committer %s\n\n"
2051
+ "Version of %s from %s\n",
2052
+ sha1_to_hex(head_sha1),
2053
+ ident, ident, path, contents_from ? contents_from : path);
2054
+ return commit;
2055
+}
2056
+
2057
int cmd_blame(int argc, const char **argv, const char *prefix)
2058
{
2059
struct rev_info revs;
@@ -1924,6 +2068,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2068
const char *final_commit_name = NULL;
2069
char type[10];
2070
const char *bottomtop = NULL;
2071
+ const char *contents_from = NULL;
2072
2073
git_config(git_blame_config);
2074
save_commit_buffer = 0;
@@ -1968,6 +2113,11 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2113
die("More than one '-L n,m' option given");
2114
bottomtop = arg;
2115
}
2116
+ else if (!strcmp("--contents", arg)) {
2117
+ if (++i >= argc)
2118
+ usage(blame_usage);
2119
+ contents_from = argv[i];
2120
+ }
2121
else if (!strcmp("--incremental", arg))
2122
incremental = 1;
2123
else if (!strcmp("--score-debug", arg))
@@ -2087,7 +2237,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2237
argv[unk] = NULL;
2238
2239
init_revisions(&revs, NULL);
2090
- setup_revisions(unk, argv, &revs, "HEAD");
2240
+ setup_revisions(unk, argv, &revs, NULL);
2241
memset(&sb, 0, sizeof(sb));
2242
2243
/*
@@ -2114,16 +2264,14 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2264
if (!sb.final) {
2265
/*
2266
* "--not A B -- path" without anything positive;
2117
- * default to HEAD.
2267
+ * do not default to HEAD, but use the working tree
2268
+ * or "--contents".
2269
*/
2119
- unsigned char head_sha1[20];
2120
-
2121
- final_commit_name = "HEAD";
2122
- if (get_sha1(final_commit_name, head_sha1))
2123
- die("No such ref: HEAD");
2124
- sb.final = lookup_commit_reference(head_sha1);
2125
- add_pending_object(&revs, &(sb.final->object), "HEAD");
2270
+ sb.final = fake_working_tree_commit(path, contents_from);
2271
+ add_pending_object(&revs, &(sb.final->object), ":");
2272
}
2273
+ else if (contents_from)
2274
+ die("Cannot use --contents with final commit object name");
2275
2276
/*
2277
* If we have bottom, this will mark the ancestors of the
@@ -2132,11 +2280,22 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
2280
*/
2281
prepare_revision_walk(&revs);
2282
2135
- o = get_origin(&sb, sb.final, path);
2136
- if (fill_blob_sha1(o))
2137
- die("no such path %s in %s", path, final_commit_name);
2283
+ if (is_null_sha1(sb.final->object.sha1)) {
2284
+ char *buf;
2285
+ o = sb.final->util;
2286
+ buf = xmalloc(o->file.size + 1);
2287
+ memcpy(buf, o->file.ptr, o->file.size + 1);
2288
+ sb.final_buf = buf;
2289
+ sb.final_buf_size = o->file.size;
2290
+ }
2291
+ else {
2292
+ o = get_origin(&sb, sb.final, path);
2293
+ if (fill_blob_sha1(o))
2294
+ die("no such path %s in %s", path, final_commit_name);
2295
2139
- sb.final_buf = read_sha1_file(o->blob_sha1, type, &sb.final_buf_size);
2296
+ sb.final_buf = read_sha1_file(o->blob_sha1, type,
2297
+ &sb.final_buf_size);
2298
+ }
2299
num_read_blob++;
2300
lno = prepare_lines(&sb);
2301
diff-lib.c
+43
-1
@@ -7,6 +7,7 @@
7
#include "diff.h"
8
#include "diffcore.h"
9
#include "revision.h"
10
+#include "cache-tree.h"
11
12
/*
13
* diff-files
@@ -271,7 +272,7 @@ static int diff_cache(struct rev_info *revs,
272
break;
273
}
274
/* Show difference between old and new */
274
- show_modified(revs,ac[1], ce, 1,
275
+ show_modified(revs, ac[1], ce, 1,
276
cached, match_missing);
277
break;
278
case 1:
@@ -372,3 +373,44 @@ int run_diff_index(struct rev_info *revs, int cached)
373
diff_flush(&revs->diffopt);
374
return ret;
375
}
376
+
377
+int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
378
+{
379
+ struct tree *tree;
380
+ struct rev_info revs;
381
+ int i;
382
+ struct cache_entry **dst;
383
+ struct cache_entry *last = NULL;
384
+
385
+ /*
386
+ * This is used by git-blame to run diff-cache internally;
387
+ * it potentially needs to repeatedly run this, so we will
388
+ * start by removing the higher order entries the last round
389
+ * left behind.
390
+ */
391
+ dst = active_cache;
392
+ for (i = 0; i < active_nr; i++) {
393
+ struct cache_entry *ce = active_cache[i];
394
+ if (ce_stage(ce)) {
395
+ if (last && !strcmp(ce->name, last->name))
396
+ continue;
397
+ cache_tree_invalidate_path(active_cache_tree,
398
+ ce->name);
399
+ last = ce;
400
+ ce->ce_mode = 0;
401
+ ce->ce_flags &= ~htons(CE_STAGEMASK);
402
+ }
403
+ *dst++ = ce;
404
+ }
405
+ active_nr = dst - active_cache;
406
+
407
+ init_revisions(&revs, NULL);
408
+ revs.prune_data = opt->paths;
409
+ tree = parse_tree_indirect(tree_sha1);
410
+ if (!tree)
411
+ die("bad tree object %s", sha1_to_hex(tree_sha1));
412
+ if (read_tree(tree, 1, opt->paths))
413
+ return error("unable to read tree %s", sha1_to_hex(tree_sha1));
414
+ return diff_cache(&revs, active_cache, active_nr, revs.prune_data,
415
+ 1, 0);
416
+}
diff.h
+1
@@ -222,6 +222,7 @@ extern int run_diff_files(struct rev_info *revs, int silent_on_removed);
222
223
extern int run_diff_index(struct rev_info *revs, int cached);
224
225
+extern int do_diff_cache(const unsigned char *, struct diff_options *);
226
extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
227
228
#endif /* DIFF_H */