forked from FabriceSalvaire/CodeReview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit2-python
More file actions
executable file
·139 lines (106 loc) · 4.2 KB
/
git2-python
File metadata and controls
executable file
·139 lines (106 loc) · 4.2 KB
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
#! /usr/bin/env python3
####################################################################################################
from __future__ import print_function
import pygit2 as git
import os
import sys
####################################################################################################
if len(sys.argv) != 2:
print("Give a repository path")
sys.exit(1)
source_path = sys.argv[1]
source_path = os.path.realpath(os.path.expandvars(source_path))
print("Repository:", source_path)
####################################################################################################
def pretty_print_commit(commit):
message_template = """
==================================================
Commit:
hex: %s
author: %s
committer: %s
parents: %s
message: %s
"""
print(message_template % (commit.hex,
commit.author.name,
commit.committer.name,
str([x.hex for x in commit.parents]),
commit.message,
))
# print(commit.tree)
####################################################################################################
# def walk_from_commit(commit):
# pretty_print_commit(commit)
# number_of_parents = len(commit.parents)
# if number_of_parents == 1:
# show_diff(commit.parents[0].tree, commit.tree)
# elif number_of_parents > 1:
# print("More than one parents")
# for parent_commit in commit.parents:
# walk_from_commit(parent_commit)
####################################################################################################
def walk_from_tree(tree):
for entry in tree:
print(entry.hex, entry.name, entry.attributes)
obj = entry.to_object()
if obj.type == git.GIT_OBJ_BLOB:
print(entry.to_object().data)
elif obj.type == git.GIT_OBJ_TREE:
walk_from_tree(obj)
####################################################################################################
# translate_diff = {
# git.GIT_DELTA_UNMODIFIED:'Unmodified',
# git.GIT_DELTA_ADDED:'Added',
# git.GIT_DELTA_DELETED:'Deleted',
# git.GIT_DELTA_MODIFIED:'Modified',
# git.GIT_DELTA_RENAMED:'Renamed',
# git.GIT_DELTA_COPIED:'Copied',
# git.GIT_DELTA_IGNORED:'Ignored',
# git.GIT_DELTA_UNTRACKED:'Untracked',
# }
# def show_diff(tree1, tree2):
# diff = tree1.diff(tree2)
# for file_diff in diff.changes['files']:
# print(' '*2, translate_diff[file_diff[2]], file_diff[0])
####################################################################################################
repository_path = git.discover_repository(source_path)
repository = git.Repository(repository_path)
# head = repository.lookup_reference('HEAD').resolve()
# head = repository.head
# head_commit = repository[head.target]
# pretty_print_commit(head_commit)
# for commit in repository.walk(head_commit.id, git.GIT_SORT_TIME):
# pretty_print_commit(commit)
# walk_from_commit(head_commit)
# walk_from_tree(head_commit.tree)
translate_status = {
git.GIT_STATUS_CURRENT:'Current',
git.GIT_STATUS_IGNORED:'Ignored',
git.GIT_STATUS_INDEX_DELETED:'Index Deleted',
git.GIT_STATUS_INDEX_MODIFIED:'Index Modified',
git.GIT_STATUS_INDEX_NEW:'Index New',
git.GIT_STATUS_WT_DELETED:'Wt Deleted',
git.GIT_STATUS_WT_MODIFIED:'Wt Modified',
git.GIT_STATUS_WT_NEW:'Wt New',
}
print("\nStatus:")
git_status = repository.status()
for filepath, status in git_status.items():
print(translate_status.get(status, status), filepath)
print("\nDiff:")
#diff = repository.diff() # not stagged
#diff = repository.diff('HEAD', cached=True) # stagged
diff = repository.diff('HEAD') # all
for patch in diff:
print(patch.old_file_path, patch.new_file_path, patch.status)
# print("\nDiff:")
# commit = repository.revparse_single('HEAD^')
# diff = repository.diff(head_commit, commit)
# for patch in diff:
# print(patch.old_file_path, patch.new_file_path, patch.status)
####################################################################################################
#
# End
#
####################################################################################################