@@ -133,7 +133,133 @@ the Python code that uses GitPython.
133133
134134
135135## Read Repository and Commit Data
136+ Create a new Python file named ` read_repo.py ` and open it so we can start
137+ to code up a simple script.
136138
139+ Start with a couple of imports and a constant:
140+
141+ ``` python
142+ import os
143+ from git import Repo
144+
145+
146+ COMMITS_TO_PRINT = 5
147+
148+ ```
149+
150+ The ` os ` module makes it easy to read environment variables, such as our
151+ ` GIT_REPO_PATH ` variable we set earlier. ` from git import Repo ` gives our
152+ application access to the GitPython library when we create the ` Repo ` object.
153+ ` COMMITS_TO_PRINT ` is a constant that limits the number of lines of output
154+ based on the amount of commits we want our script to print information on.
155+ Full Stack Python has over 2,250 commits so there'd be a whole lot of output
156+ if we printed every commit.
157+
158+ Next within our ` read_repo.py ` file create a function to print individual
159+ commit information:
160+
161+ ``` python
162+ def print_commit (commit ):
163+ print (' ----' )
164+ print (str (commit.hexsha))
165+ print (" \" {} \" by {} ({} )" .format(commit.summary,
166+ commit.author.name,
167+ commit.author.email))
168+ print (str (commit.authored_datetime))
169+ print (str (" count: {} and size: {} " .format(commit.count(),
170+ commit.size)))
171+
172+
173+ ```
174+
175+
176+
177+ ``` python
178+ def print_repository (repo ):
179+ print (' Repo active branch is {} ' .format(repo.active_branch))
180+ print (' Repo description: {} ' .format(repo.description))
181+ print (' Repo active branch is {} ' .format(repo.active_branch))
182+ for remote in repo.remotes:
183+ print (' Remote named "{} " with URL "{} "' .format(remote, remote.url))
184+ print (' Last commit for repo is {} .' .format(str (repo.head.commit.hexsha)))
185+ ```
186+
187+ Finally, we need a "main" function for when we invoke the script from the
188+ terminal using the ` python ` command. Round out our
189+
190+ ``` python
191+
192+ if __name__ == " __main__" :
193+ repo_path = os.getenv(' GIT_REPO_PATH' )
194+ # Repo object used to programmatically interact with Git repositories
195+ repo = Repo(repo_path)
196+ # check that the repository loaded correctly
197+ if not repo.bare:
198+ print (' Repo at {} successfully loaded.' .format(repo_path))
199+ print_repository(repo)
200+ # create list of commits then print some of them to stdout
201+ commits = list (repo.iter_commits(' master' ))[:COMMITS_TO_PRINT ]
202+ for commit in commits:
203+ print_commit(commit)
204+ pass
205+ else :
206+ print (' Could not load repository at {} :(' .format(repo_path))
207+ ```
208+
209+
210+
211+ If you want to copy and paste all of the code found above at once, take a
212+ look at the
213+ [ ` read_repo.py ` file on GitHub] ( https://github.com/fullstackpython/blog-code-examples/blob/master/first-steps-gitpython/read_repo.py ) .
214+
215+ Time to test our GitPython-using script. Invoke the ` read_repo.py ` file using
216+ the following command.
217+
218+ ``` bash
219+ (gitpy) $ python read_repo.py
220+ ```
221+
222+ If the virtualenv is activated and the ` GIT_REPO_PATH ` environment variable
223+ is set properly, we should see output similar to the following.
224+
225+ ``` bash
226+ (gitpy) $ python read_repo.py
227+ Repo at ~ /devel/py/fsp/ successfully loaded.
228+ Repo active branch is master
229+ Repo description: Unnamed repository; edit this file ' description' to name the repository.
230+ Repo active branch is master
231+ Remote named " origin" with URL " git@github.com:mattmakai/fullstackpython.com"
232+ Last commit for repo is 1b026e4268d3ee1bd55f1979e9c397ca99bb5864.
233+ ----
234+ 1b026e4268d3ee1bd55f1979e9c397ca99bb5864
235+ " new blog post, just needs completed code section" by Matt Makai (matthew.makai@gmail.com)
236+ 2017-11-30 09:00:06-05:00
237+ count: 2255 and size: 269
238+ ----
239+ 2136d845de6f332505c3df38efcfd4c7d84a45e2
240+ " change previous email newsletters list style" by Matt Makai (matthew.makai@gmail.com)
241+ 2017-11-20 11:44:13-05:00
242+ count: 2254 and size: 265
243+ ----
244+ 9df077a50027d9314edba7e4cbff6bb05c433257
245+ " ensure picture sizes are reasonable" by Matt Makai (matthew.makai@gmail.com)
246+ 2017-11-14 13:29:39-05:00
247+ count: 2253 and size: 256
248+ ----
249+ 3f6458c80b15f58a6e6c85a46d06ade72242c572
250+ " add databases logos to relational databases pagem" by Matt Makai (matthew.makai@gmail.com)
251+ 2017-11-14 13:28:02-05:00
252+ count: 2252 and size: 270
253+ ----
254+ c76810c85387d2d63edd83ea715d8d46f294c63b
255+ " fix broken changelog link" by Matt Makai (matthew.makai@gmail.com)
256+ 2017-11-14 13:17:45-05:00
257+ count: 2251 and size: 246
258+ ```
259+
260+ The specific commits you see will vary based on the last 5 commits I've
261+ pushed to the GitHub repository, but if you see something like the output
262+ above that is a good sign everything worked as expected.
137263
138264
139265## What's next?
0 commit comments