summaryrefslogtreecommitdiffstats
path: root/volgit.c
blob: 27b96fb461d7a3bf40c16598cd85f4e391e31d3f (plain)
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
#include <stdio.h>
#include <git2.h>

int print_diff_line(const git_diff_delta *delta,const git_diff_hunk *hunk,const git_diff_line *line,void *payload)
{
	size_t i;
	for(i=0;i<line->content_len;++i)
		printf("%c",line->content[i]);
	printf("\n"); 
	return 0;
}

void print_diff(git_tree *parent_tree,git_tree *current_tree,git_repository *repo)
{
	git_diff *diff_from_parent;
	size_t number_of_deltas=0;
	size_t i;

	git_diff_tree_to_tree(&diff_from_parent,repo,current_tree,parent_tree,NULL);

	git_diff_print(diff_from_parent,GIT_DIFF_FORMAT_PATCH,print_diff_line,NULL);

	if(diff_from_parent)
		git_diff_free(diff_from_parent);
}
void print_headers_and_commit_message(git_commit *current_commit,git_oid *current)
{
	const git_signature *who_commited;
	printf("COMMIT: %s\n",git_oid_tostr_s(current));

	who_commited=git_commit_committer(current_commit);

	printf("AUTHOR: %s <%s>\n",who_commited->name,who_commited->email);

	printf("DATE: %s\n",ctime(&who_commited->when.time));


	printf("\t%s\n",git_commit_message(current_commit));
}
void print_commits(const git_reference *branch, git_repository *repo)
{
	const git_oid *id;
	git_revwalk *walker;
	git_oid current;
	git_commit *current_commit;
	git_time_t time_of_commit;
	git_tree *parent_tree=NULL;
	git_tree *current_tree;

	git_revwalk_new(&walker,repo);
	id=git_reference_target(branch);
	git_revwalk_push(walker,id);
	
	while(!git_revwalk_next(&current,walker))
	{
		git_commit_lookup(&current_commit,repo,&current);
		git_commit_tree(&current_tree,current_commit);
		if(parent_tree!=NULL)
		{
			print_diff(current_tree,parent_tree,repo);
			git_tree_free(parent_tree);
		}
		
		print_headers_and_commit_message(current_commit,&current);

		parent_tree=current_tree;
		git_commit_free(current_commit);
	}


}

int main()
{
	const char *name;

	git_branch_iterator *it;
	git_repository *repo;
	git_reference *ref;
	git_branch_t branch_type=GIT_BRANCH_LOCAL;


	git_libgit2_init();
	git_repository_open(&repo,".");
	git_branch_iterator_new(&it,repo,branch_type);


	while(git_branch_next(&ref,&branch_type,it)==0)
	{
		git_branch_name(&name,ref);	
		if(name)
		{
			printf("------- %s -------\n",name);
			print_commits(ref,repo);	
			printf("------------------\n");
		}else
		{
			printf("NULL\n");
		}
	}




	git_repository_free(repo);
	git_branch_iterator_free(it);
	git_libgit2_shutdown();

	return 0;
}