Skip to contentSkip to conversation

Add git-annotate, a tool for assigning blame.

PATCH In review Ryan Anderson 5 participants 13 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add git-annotate, a tool for assigning blame.

---

(Pull from http://h4x0r5.com/~ryan/git/ryan.git/ annotate-upstream )

I'm pretty sure this version (finally) gets the edge cases correct.

I would appreciate some other testing on this, as I can't find a case where it falls down, but the files with a lot of history tend to have a lot of lines, making them hard to spotcheck without having been an intimate part of that history.

Oh, this is the "functional" version, but it might not qualify as "nice looking" yet, pleaes, feel free to complain.

 Makefile          |    1 
 git-annotate.perl |  321 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 322 insertions(+), 0 deletions(-)
 create mode 100755 git-annotate.perl

107045e8abb674a66ee7c682dd85a3d303f26e3c

Makefile +1 -0
index 317be3c..86ffcf4 100644
@@ -119,6 +119,7 @@ SCRIPT_SH = \
 SCRIPT_PERL = \
 	git-archimport.perl git-cvsimport.perl git-relink.perl \
 	git-shortlog.perl git-fmt-merge-msg.perl git-rerere.perl \
+	git-annotate.perl \
 	git-svnimport.perl git-mv.perl git-cvsexportcommit.perl
 
 SCRIPT_PYTHON = \
git-annotate.perl new file +321 -0

321 lines not on this page · raw message

Signature · 1 line
1.2.2.gb342
#1 lore raw
Quoted 8 lines · Ryan Anderson

---

(Pull from http://h4x0r5.com/~ryan/git/ryan.git/ annotate-upstream )

I'm pretty sure this version (finally) gets the edge cases correct.

Quoted 8 lines

I would appreciate some other testing on this, as I can't find a case where it falls down, but the files with a lot of history tend to have a lot of lines, making them hard to spotcheck without having been an intimate part of that history.

Oh, this is the "functional" version, but it might not qualify as "nice looking" yet, pleaes, feel free to complain.

Nice work!

I have also been working on a blame program. The algorithm is pretty much the one described by Junio in his blame.perl. My variant doesn't handle renames, but it shouldn't be too hard to add that. The output is minimal, just the line number followed by the commit SHA1.

An interesting observation is that the output from my git-blame and your git-annotate doesn't match on all files in the git repository. One example where several lines differ is read-cache.c. I haven't investigated it further to find out which one is correct.

The code should be considered as a work in progress. It certainly has a couple of rough edges. The output looks fairly sane on the few files I have tested it on, but it wouldn't be too surprising if it gets some cases wrong.

---

 Makefile |    2 
 blame.c  |  444 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 445 insertions(+), 1 deletions(-)
 create mode 100644 blame.c

afe1466a4f94bc72b42dce5fd1fed480c5aa4c49

Makefile +1 -1
index 317be3c..4a3ffe9 100644
@@ -153,7 +153,7 @@ PROGRAMS = \
 	git-upload-pack$X git-verify-pack$X git-write-tree$X \
 	git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
 	git-name-rev$X git-pack-redundant$X git-repo-config$X git-var$X \
-	git-describe$X
+	git-describe$X git-blame$X
 
 # what 'all' will build and 'install' will install, in gitexecdir
 ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS)
blame.c new file +444 -0

444 lines not on this page · raw message

Signature · 1 line
1.2.1.g62a4-dirty
#2 lore raw
Quoted 1 line · Fredrik Kuivinen

I have also been working on a blame program.

Very nice to see these two.

Obviously I prefer this one for its performance ;-).

Its interface is probably friendlier when used as a preprocessor by other tools. I can imagine GUI source viewer that fontifies the source code and prefers to get just the origin information from a "blame backend".

BTW, these days I always compile things with

-Wall -Wdeclaration-after-statement

which caught quite a many.

Also I have my templates/hooks--pre-commit enabled so you might want to lindent it before inclusion.

I'll play with both a bit. Thanks for nice toys, both of you!

#12 lore raw
In reply to Ryan Anderson
Quoted 4 lines · Ryan Anderson

I would appreciate some other testing on this, as I can't find a case where it falls down, but the files with a lot of history tend to have a lot of lines, making them hard to spotcheck without having been an intimate part of that history.

I looked at a couple of files, including pack-objects.c, rev-list.c (in "pu"), and svnimport.perl; what I saw made sense. I think however we would want to check things with a lot of merges, so git repository may not be a good guinea pig.

Quoted 2 lines

Oh, this is the "functional" version, but it might not qualify as "nice looking" yet, pleaes, feel free to complain.

Nice.

Two design glitches and an implementation:

- You seem to rely on the working tree file to be clean relative to HEAD.

- You do not take anything other than HEAD as the starting point.

Maybe an additional -r<this-version> option which defaults to HEAD, plus reading the blob always from that tree as the starting point would be helpful.

git-annotate.perl line 122

Quoted 1 line
git-annotate.perl new file +1 -0
+	open(P,"-|","git-rev-list","--parents","--remove-empty",$rev,"--",$file)

Johannes noticed I slipped this form which Perl 5.6 does not grok in another program. Eric pointed out he uses a backward compatible idiom in his code.

We would need to make this one safe; something like this, perhaps:

	sub open_read_pipe {
        	my ($fh, @cmd) = @_;
                my $pid = open($fh, '-|');
		die "$!" unless defined($pid);
		if (!$pid) {
			exec(@cmd) or die "$!";
		}
		return $fh;
	}
	...
        open_read_pipe(\*P, qw(git-rev-list --parents --remove-empty),
			$rev, '--', $file);

git-annotate.perl line 141

Quoted 2 lines
git-annotate.perl new file +2 -0
+	open(P,"-|","git-diff-tree", "-M50", "-r","--name-status", "-z","$rev")
+		or die "Failed to exec git-diff: $!";

If you do not mean "I want -M50" but you meant "I want whatever is default", I'd leave that 50 out.

It is probably premature to talk about issues for UI that can be built on top of this, but what I found interesting was this. While looking at the annotate output, whenever I got curious about one line ("Oh, this is an ancient change and by somebody who does not feed patches to git regularly -- what was the change about???"), I grabbed the SHA1 of the commit on the line and threw it at "git show". It was a good way to see how the change to the line was done in context. Maybe a two-pane UI that shows annotate on the top pane, and activating one line from it shows "git show" output on the bottom pane to show the commit log plus changes the commit introduced to the file and other files.

#3 lore raw
In reply to Junio C Hamano
Quoted 3 lines · Junio C Hamano
Quoted 1 line · Fredrik Kuivinen

I have also been working on a blame program.

...

Quoted 5 lines

BTW, these days I always compile things with

-Wall -Wdeclaration-after-statement

which caught quite a many.

Just out of curiosity, why do you prefer declarations before statements?

- Fredrik

#4 lore raw
Quoted 7 lines · Fredrik Kuivinen
Quoted 3 lines · Junio C Hamano
Quoted 1 line · Fredrik Kuivinen

I have also been working on a blame program.

...

Quoted 8 lines
Quoted 5 lines

BTW, these days I always compile things with

-Wall -Wdeclaration-after-statement

which caught quite a many.

Just out of curiosity, why do you prefer declarations before statements?

I won't speak for Junio, but the explanations I've heard in the past are basically:

1) It keeps all declarations in one spot.
2) If your function is complicated enough to not need a variable until fairly far into the function, it probably should be two (or more) functions.

Basically, I think that there's not wrong with doing it that way, per se, just that it's sometimes a symptom of other problems, so fi you look for the symptom, the problem sometimes is more obvious.

Signature · 2 lines
Ryan Anderson
  sometimes Pug Majere
#5 lore raw
Quoted 22 lines · Ryan Anderson
Quoted 10 lines · Fredrik Kuivinen
Quoted 4 lines · Junio C Hamano
Quoted 1 line · Fredrik Kuivinen

I have also been working on a blame program.

...

Quoted 8 lines
Quoted 5 lines

BTW, these days I always compile things with

-Wall -Wdeclaration-after-statement

which caught quite a many.

Just out of curiosity, why do you prefer declarations before statements?

Quoted 7 lines

I won't speak for Junio, but the explanations I've heard in the past are basically:

1) It keeps all declarations in one spot.
2) If your function is complicated enough to not need a variable until fairly far into the function, it probably should be two (or more) functions.

3) Not all compilers support it.

Signature · 3 lines
Andreas Ericsson                   [email protected]
OP5 AB                             www.op5.se
Tel: +46 8-230225                  Fax: +46 8-230231
#6 lore raw
In reply to Fredrik Kuivinen
Quoted 10 lines · Fredrik Kuivinen
Quoted 6 lines · Junio C Hamano

BTW, these days I always compile things with

-Wall -Wdeclaration-after-statement

which caught quite a many.

Just out of curiosity, why do you prefer declarations before statements?

Inertia, IOW, mostly being used to read code written that way.

#7 lore raw
In reply to Fredrik Kuivinen

(Biased critique since I have the other tool in the tree, but still...)

Quoted 55 lines · Fredrik Kuivinen
blame.c new file +49 -0
new file mode 100644
index 0000000..d4a2fad
@@ -0,0 +1,444 @@
+#include <assert.h>
+
+#include "cache.h"
+#include "refs.h"
+#include "tag.h"
+#include "commit.h"
+#include "tree.h"
+#include "blob.h"
+#include "epoch.h"
+#include "diff.h"
+
+#define DEBUG 0
+
+struct commit** blame_lines;
+int num_blame_lines;
+
+struct util_info
+{
+    int* line_map;
+    int num_lines;
+    unsigned char sha1[20]; /* blob sha, not commit! */
+    char* buf;
+    unsigned long size;
+//    const char* path;
+};
+
+struct chunk
+{
+    int off1, len1; // ---
+    int off2, len2; // +++
+};
+
+struct patch
+{
+    struct chunk* chunks;
+    int num;
+};
+
+static void get_blob(struct commit* commit);
+
+int num_get_patch = 0;
+int num_commits = 0;
+
+struct patch* get_patch(struct commit* commit, struct commit* other)
+{
+    struct patch* ret = xmalloc(sizeof(struct patch));
+    ret->chunks = NULL;
+    ret->num = 0;
+

blame.c line 59

Quoted 10 lines
blame.c new file +10 -0
+    struct util_info* info_c = (struct util_info*) commit->object.util;
+    struct util_info* info_o = (struct util_info*) other->object.util;
+
+    if(!memcmp(info_c->sha1, info_o->sha1, 20))
+        return ret;
+
+    get_blob(commit);
+    get_blob(other);
+
+    FILE* fout = fopen("/tmp/git-blame-tmp1", "w");

Probably should be using something like mkstemp (mkstmp?) here, so if someone is runnign things as root or with a malicous user around, things don't collide.

Hell, so on a multi-user machine this doesn't blow up on you.

But, read down for a related comment.

Quoted 8 lines
blame.c new file +8 -0
+    if(!fout)
+        die("fopen tmp1 failed: %s", strerror(errno));
+
+    if(fwrite(info_c->buf, info_c->size, 1, fout) != 1)
+        die("fwrite 1 failed: %s", strerror(errno));
+    fclose(fout);
+
+    fout = fopen("/tmp/git-blame-tmp2", "w");

blame.c line 77

Quoted 10 lines
blame.c new file +10 -0
+    if(!fout)
+        die("fopen tmp2 failed: %s", strerror(errno));
+
+    if(fwrite(info_o->buf, info_o->size, 1, fout) != 1)
+        die("fwrite 2 failed: %s", strerror(errno));
+    fclose(fout);
+
+    FILE* fin = popen("diff -u0 /tmp/git-blame-tmp1 /tmp/git-blame-tmp2", "r");
+    if(!fin)
+        die("popen failed: %s", strerror(errno));

Can't git-diff-tree do this sufficiently, anyway? See my Perl script for an example, you just need both commit IDs and both filenames and the appropriate -M and you get the right results.

(It's possible that's part of where the performance differences are, though, not really sure at the moment.)

I'm going to stop there for the moment, I'm not really confident in my understanding of git-internals to say much more just yet.

This could probably benefit a *LOT* from the libification project, I think, though.

Signature · 2 lines
Ryan Anderson
  sometimes Pug Majere
#8 lore raw
Quoted 9 lines · Ryan Anderson

(Biased critique since I have the other tool in the tree, but still...)

Quoted 1 line

+ FILE* fout = fopen("/tmp/git-blame-tmp1", "w");

Probably should be using something like mkstemp (mkstmp?) here, so if someone is runnign things as root or with a malicous user around, things don't collide.

Hell, so on a multi-user machine this doesn't blow up on you.

Yep, I know. The code is mostly a proof of concept. I didn't submit it for inclusion.

Quoted 26 lines

But, read down for a related comment.

Quoted 8 lines
blame.c new file +8 -0
+    if(!fout)
+        die("fopen tmp1 failed: %s", strerror(errno));
+
+    if(fwrite(info_c->buf, info_c->size, 1, fout) != 1)
+        die("fwrite 1 failed: %s", strerror(errno));
+    fclose(fout);
+
+    fout = fopen("/tmp/git-blame-tmp2", "w");

blame.c line 77

Quoted 10 lines
blame.c new file +10 -0
+    if(!fout)
+        die("fopen tmp2 failed: %s", strerror(errno));
+
+    if(fwrite(info_o->buf, info_o->size, 1, fout) != 1)
+        die("fwrite 2 failed: %s", strerror(errno));
+    fclose(fout);
+
+    FILE* fin = popen("diff -u0 /tmp/git-blame-tmp1 /tmp/git-blame-tmp2", "r");
+    if(!fin)
+        die("popen failed: %s", strerror(errno));

Can't git-diff-tree do this sufficiently, anyway? See my Perl script for an example, you just need both commit IDs and both filenames and the appropriate -M and you get the right results.

Quoted 3 lines

(It's possible that's part of where the performance differences are, though, not really sure at the moment.)

Yeah.. maybe. My first thought was to avoid forking and execing diff and use some C library for doing the diffing instead (libxdiff). But then I just wanted to get some code working and the simplest solution I could think of was to fork and exec diff.

Quoted 5 lines

I'm going to stop there for the moment, I'm not really confident in my understanding of git-internals to say much more just yet.

This could probably benefit a *LOT* from the libification project, I think, though.

Yes, perhaps. Some of the git-rev-list bits might simplify a couple of things.

I have found some severe problems with the code I posted, in particular it doesn't handle parallel development tracks at all. I am working on fixing it, but it isn't finished yet.

Thanks for the comments.

- Fredrik

#9 lore raw

Hi,

Quoted 5 lines · Fredrik Kuivinen
Quoted 4 lines · Ryan Anderson

(Biased critique since I have the other tool in the tree, but still...)

Quoted 1 line

+ FILE* fout = fopen("/tmp/git-blame-tmp1", "w");

Quoted 8 lines
Quoted 5 lines

Probably should be using something like mkstemp (mkstmp?) here, so if someone is runnign things as root or with a malicous user around, things don't collide.

Hell, so on a multi-user machine this doesn't blow up on you.

Yep, I know. The code is mostly a proof of concept. I didn't submit it for inclusion.

Ha ha, famous last words!

Quoted 25 lines
Quoted 25 lines

But, read down for a related comment.

Quoted 8 lines
blame.c new file +8 -0
+    if(!fout)
+        die("fopen tmp1 failed: %s", strerror(errno));
+
+    if(fwrite(info_c->buf, info_c->size, 1, fout) != 1)
+        die("fwrite 1 failed: %s", strerror(errno));
+    fclose(fout);
+
+    fout = fopen("/tmp/git-blame-tmp2", "w");

blame.c line 77

Quoted 10 lines
blame.c new file +10 -0
+    if(!fout)
+        die("fopen tmp2 failed: %s", strerror(errno));
+
+    if(fwrite(info_o->buf, info_o->size, 1, fout) != 1)
+        die("fwrite 2 failed: %s", strerror(errno));
+    fclose(fout);
+
+    FILE* fin = popen("diff -u0 /tmp/git-blame-tmp1 /tmp/git-blame-tmp2", "r");
+    if(!fin)
+        die("popen failed: %s", strerror(errno));

Can't git-diff-tree do this sufficiently, anyway? See my Perl script for an example, you just need both commit IDs and both filenames and the appropriate -M and you get the right results.

Quoted 8 lines
Quoted 3 lines

(It's possible that's part of where the performance differences are, though, not really sure at the moment.)

Yeah.. maybe. My first thought was to avoid forking and execing diff and use some C library for doing the diffing instead (libxdiff). But then I just wanted to get some code working and the simplest solution I could think of was to fork and exec diff.

git-diff-tree fork()s a diff. So, by fork()ing git-diff-tree you get two fork()s (and no knife...).

Quoted 8 lines
Quoted 5 lines

I'm going to stop there for the moment, I'm not really confident in my understanding of git-internals to say much more just yet.

This could probably benefit a *LOT* from the libification project, I think, though.

Yes, perhaps. Some of the git-rev-list bits might simplify a couple of things.

The major problem is probably not solved: What Linus calls a "stream interface".

I.e. if you pipe the output of git-rev-list to another program, you *need* to execute the two semi-simultaneously. The "alternative" would be to use buffers, which can get huge (and are sometimes not needed: think git-whatchanged, which starts outputting before it's getting no more input).

Quoted 3 lines

I have found some severe problems with the code I posted, in particular it doesn't handle parallel development tracks at all. I am working on fixing it, but it isn't finished yet.

Looking forward to them!

Ciao,
Dscho

#10 lore raw
Quoted 6 lines · Johannes Schindelin
Quoted 5 lines
Quoted 2 lines

This could probably benefit a *LOT* from the libification project, I think, though.

Yes, perhaps. Some of the git-rev-list bits might simplify a couple of things.

Quoted 8 lines

The major problem is probably not solved: What Linus calls a "stream interface".

I.e. if you pipe the output of git-rev-list to another program, you *need* to execute the two semi-simultaneously. The "alternative" would be to use buffers, which can get huge (and are sometimes not needed: think git-whatchanged, which starts outputting before it's getting no more input).

You need a limited coroutine support, something like generator functions in Python ;-). In C, traditional way of doing it is to make your application specific function a callback of rev-list or whatever generator is, which is very unpleasant to code.

Quoted 5 lines
Quoted 3 lines

I have found some severe problems with the code I posted, in particular it doesn't handle parallel development tracks at all. I am working on fixing it, but it isn't finished yet.

Looking forward to them!

Likewise.

#11 lore raw

Hi,

Quoted 17 lines · Junio C Hamano
Quoted 6 lines · Johannes Schindelin
Quoted 5 lines
Quoted 2 lines

This could probably benefit a *LOT* from the libification project, I think, though.

Yes, perhaps. Some of the git-rev-list bits might simplify a couple of things.

Quoted 8 lines

The major problem is probably not solved: What Linus calls a "stream interface".

I.e. if you pipe the output of git-rev-list to another program, you *need* to execute the two semi-simultaneously. The "alternative" would be to use buffers, which can get huge (and are sometimes not needed: think git-whatchanged, which starts outputting before it's getting no more input).

Quoted 5 lines

You need a limited coroutine support, something like generator functions in Python ;-). In C, traditional way of doing it is to make your application specific function a callback of rev-list or whatever generator is, which is very unpleasant to code.

The most unpleasant aspect is that you usually need something like "this" in C++: a pointer to an object (which you have to pass around all the time). Without it you can not use the function in a nested way.

However, I can also see benefits of this when compared to the traditional UNIX approach. It should be faster, for one, since you don't need to pass data through pipes all the time. (This might be not as true for Linux as for other OSes.)

Ciao,
Dscho