summaryrefslogtreecommitdiffstats
path: root/git_part.c
blob: 046720602792a98d8b7672fabc472b599c602f8e (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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef VOLGIT_GIT_PART_C
#define VOLGIT_GIT_PART_C VOLGIT_GIT_PART_C
#include <git_part.h>

int print_diff_line(const git_diff_delta *delta,const git_diff_hunk *hunk,const git_diff_line *line,FILE *out)
{
	size_t i;
	fprintf(out,"%c ",line->origin);
	for(i=0;i<line->content_len;++i)
		fprintf(out,"%c",line->content[i]);
	return 0;
}

void print_diff(FILE *out,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_ID,(git_diff_line_cb)print_diff_line,out);

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

	who_commited=git_commit_committer(current_commit);

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

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


	fprintf(where,"\t%s\n",git_commit_message(current_commit));
}
void print_commits(int dir_fd,const git_reference *branch, git_repository *repo)
{
	const git_oid *id;
	FILE *log_file;
	FILE *diff_file;
	int diff_directory_fd;
	int files_directory_fd;

	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);


	log_file=create_file(dir_fd,"log");
	diff_directory_fd=create_dir(dir_fd,"diffs");
	files_directory_fd=create_dir(dir_fd,"files");
	
	while(!git_revwalk_next(&current,walker))
	{
		git_commit_lookup(&current_commit,repo,&current);
		git_commit_tree(&current_tree,current_commit);

		if(parent_tree!=NULL)
		{
			diff_file=create_file(diff_directory_fd,git_oid_tostr_s(&current));

			print_diff(diff_file,parent_tree,current_tree,repo);

			git_tree_free(parent_tree);
		}else/*we only print the files of the HEAD*/
		{
			print_files(files_directory_fd,current_tree,repo);
		}
		
		print_headers_and_commit_message(log_file,current_commit,&current);

		parent_tree=current_tree;
		git_commit_free(current_commit);
	}

	git_revwalk_free(walker);
	fclose(log_file);
}

void print_branches(git_repository *repo)
{
	const char *branch_name;
	int branch_dir_fd;
	git_branch_iterator *it;
	git_reference *ref;
	git_branch_t branch_type=GIT_BRANCH_LOCAL;

	git_branch_iterator_new(&it,repo,branch_type);
	while(git_branch_next(&ref,&branch_type,it)==0)
	{
		git_branch_name(&branch_name,ref);	
		if(branch_name)
		{
			branch_dir_fd=create_branch_dir(branch_name);

			print_commits(branch_dir_fd,ref,repo);	

			close(branch_dir_fd);
		}else
		{
			printf("NULL\n");
		}
	}

	git_branch_iterator_free(it);
}
void print_files(int dir_fd,git_tree *tree,git_repository *repo)
{
	size_t number_of_entries;
	size_t i;
	const git_tree_entry *current_entry;

	number_of_entries=git_tree_entrycount(tree);
	
	for(i=0;i<number_of_entries;++i)
	{
		current_entry=git_tree_entry_byindex(tree,i);
		print_entry(current_entry,dir_fd,repo);
	}
}
int print_entry(const git_tree_entry *entry,int base_dir_fd,git_repository *repo)
{
	git_object *obj;
	const char *entry_name;

	git_tree_entry_to_object(&obj,repo,entry);
	entry_name=git_tree_entry_name(entry);

	switch(git_object_type(obj))
	{
		case GIT_OBJECT_TREE:
			{
				int new_dir_fd;
				git_tree *tree;

				tree=(git_tree*)obj;
				
				new_dir_fd=create_dir(base_dir_fd,entry_name);
				print_files(new_dir_fd,tree,repo);
			}
			break;
		case GIT_OBJECT_BLOB:
			{
				FILE *blob_file;
				const void *blob_data;
				size_t blob_size;
				git_blob *blob;

				blob=(git_blob*)obj;

				blob_file=create_file(base_dir_fd,entry_name);
				blob_size=git_blob_rawsize(blob);
				blob_data=git_blob_rawcontent(blob);

				fwrite(blob_data,blob_size,1,blob_file);

				fclose(blob_file);
			}
			break;
	}

	git_object_free(obj);
	return 0;
}
#endif