Skip to content

Commit 82ee0c4

Browse files
committed
update script for blog post
1 parent 4361bfa commit 82ee0c4

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Posts and associated code:
77

88
|Post|Code Directory|Notes|
99
|---|---|---|
10+
|First Steps with GitPython|[first-step-gitpython](https://github.com/fullstackpython/blog-code-examples/tree/master/first-steps-gitpython)||
1011
|[How to Monitor Python Web Applications](https://www.fullstackpython.com/blog/blog/monitor-python-web-applications.html)|[monitor-python-bottle-apps](https://github.com/fullstackpython/blog-code-examples/tree/master/monitor-python-bottle-apps)||
1112
|[How to Provision Ubuntu 16.04 Linux Servers on Linode](https://www.fullstackpython.com/blog/provision-ubuntu-linux-servers-linode.html)|No code for this post.||
1213
|[Creating Bar Chart Visuals with Bokeh, Bottle and Python 3](https://www.fullstackpython.com/blog/python-bottle-bokeh-bar-charts.html)|[bar-charts-bokeh-bottle-python-3](https://github.com/fullstackpython/blog-code-examples/tree/master/bar-charts-bokeh-bottle-python-3)||

first-steps-gitpython/read_repo.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
from git import Repo
3+
4+
5+
COMMITS_TO_PRINT = 5
6+
7+
8+
def print_commit(commit):
9+
print('----')
10+
print(str(commit.hexsha))
11+
print("\"{}\" by {} ({})".format(commit.summary,
12+
commit.author.name,
13+
commit.author.email))
14+
print(str(commit.authored_datetime))
15+
print(str("count: {} and size: {}".format(commit.count(),
16+
commit.size)))
17+
print(str(commit.size))
18+
19+
20+
def print_repository(repo):
21+
print('Repo active branch is {}'.format(repo.active_branch))
22+
print('Repo description: {}'.format(repo.description))
23+
print('Repo active branch is {}'.format(repo.active_branch))
24+
for remote in repo.remotes:
25+
print('Remote named "{}" with URL "{}"'.format(remote, remote.url))
26+
print('Last commit for repo is {}.'.format(str(repo.head.commit.hexsha)))
27+
28+
29+
if __name__ == "__main__":
30+
repo_path = os.getenv('GIT_REPO_PATH')
31+
# Repo object used to programmatically interact with Git repositories
32+
repo = Repo(repo_path)
33+
# check that the repository loaded correctly
34+
if not repo.bare:
35+
print('Repo at {} successfully loaded.'.format(repo_path))
36+
print_repository(repo)
37+
# create list of commits then print some of them to stdout
38+
commits = list(repo.iter_commits('master'))[:COMMITS_TO_PRINT]
39+
for commit in commits:
40+
print_commit(commit)
41+
pass
42+
else:
43+
print('Could not load repository at {} :('.format(repo_path))

0 commit comments

Comments
 (0)