gitstats: Removed try/except from setupRepo but added a manual raise
[git-stats.git] / src / scripts / setupRepo.py
blob129177234e48e498d898d731c30b067ba9b91699
1 #!/usr/bin/env python
3 import os
4 import tempfile
6 from git_python import Repo
7 from git_python import Git
9 repopath = os.path.join(tempfile.gettempdir(), "freshrepo")
11 class InitializationException(Exception):
12 """This exception is raised when something goes wrong during initialization.
13 """
15 def setupFreshRepo():
16 """Creates a fresh repo under setupRepo.repopath
18 If setupRepo.repopath already exists an exception is raised.
19 Configures GIT_COMMITTER_DATE and GIT_AUTHOR_DATE to a default value.
20 This value is taken from t/t1400-update-ref.sh from git.git.
21 The actual value is "2005-05-26 23:30"
22 """
24 if os.path.exists(repopath):
25 raise InitializationException(
26 "The specified path, " + repopath + ", exists already, "
27 "please remove it or change the target path.")
29 # Create a new repo
30 repo = Repo.create(repopath)
32 # Date taken from t/t1400-update-ref.sh
33 os.environ["GIT_COMMITTER_DATE"] = "2005-05-26 23:30"
34 os.environ["GIT_AUTHOR_DATE"] = "2005-05-26 23:30"
35 os.chdir(repopath)
37 git = Git(".")
39 git.config("user.email", "sverre@rabbelier.nl")
40 git.config("user.name", "Sverre Rabbelier")
42 def createLinearHistory(filename, createFile=False, initialCommit=False, start=1, count=10, finalCommit=False):
43 """Creates a linear history in the directory of the current repository.
45 The history is fairly simple and is all acted upon the specified file.
46 Any exceptions thrown during IO operations are _not_ cought.
48 Params:
49 filename: The name of the file in which to generate the history.
50 createFile: Whether the specified file has to be created first.
51 initialCommit: Whether to create an initial commit.
52 start: Which commit to start the history at.
53 count: The size of the history.
54 finalCommit: Whether to create a final commit.
55 """
57 git = Git(".")
59 stop = start + count
61 if createFile:
62 mode = "w"
63 else:
64 mode = "a"
66 # Create or open the content file
67 file = open(filename, mode)
69 # If it is new, add it to the repository as 'git commit -a' will not do so
70 if createFile:
71 git.add(filename)
73 if initialCommit:
74 # Start out with an initial change
75 file.write("Initial change\n")
76 file.flush()
78 # Add the file and create the initial commit
79 git.commit("-a", "-m", "Initial commit")
81 # Create a linear history
82 for i in range(start, stop):
83 file.write("Change " + str(i) + "\n")
84 file.flush()
86 git.commit("-a", "-m", "Commit " + str(i))
88 if finalCommit:
89 # Finish it up with a final change
90 file.write("Last change\n")
91 file.flush()
93 # And a final commit
94 git.commit("-a", "-m", "Last commit")
96 def createBranch(name, checkoutBranch=True):
97 """Creates a branch with the specified name and optionally checks it out
99 Params:
100 name: The name of the new branch.
101 checkoutBranch: Whether to perform a checkout of the branch.
104 git = Git(".")
106 git.branch(name)
108 if checkoutBranch:
109 git.checkout(name)
111 def checkoutBranch(name):
112 """Switches to the specified branch
114 Params:
115 name: The name of the branch to be switched to.
118 git = Git(".")
120 git.checkout(name)
122 def mergeBranch(name):
123 """Merges the current branch with the specified branch
125 Params:
126 name: The name of the branch to merge with.
129 git = Git(".")
131 git.merge(name)
133 def revertLastCommit(commitChanges=True):
134 """Reverts the last commit made
136 Params:
137 commitChanges: Whether to commit the revert.
140 git = Git(".")
142 options = ["--no-edit", "HEAD"]
144 if not commitChanges:
145 options.append("-n")
147 git.revert(*options)
149 def tagRelease(releaseNumber):
150 """Tags a release.
152 The created tag is 'v' + releaseNumber.
154 Params:
155 releaseNumber: The number of the release.
158 git = Git(".")
160 git.tag('v' + str(releaseNumber))
162 def checkHead(HEAD):
163 """Verifies that the HEAD is as expected.
165 Params:
166 HEAD: The expected value of HEAD.
169 git = Git(".")
171 result = git.rev_parse("HEAD")
173 # Eat the trailing newline
174 currentHEAD = result[:-1]
176 scriptSuccessful = (HEAD == currentHEAD)
178 if scriptSuccessful:
179 print("Done, repo created successfully")
180 else:
181 print("Something went wrong, current HEAD doesn't match.")
182 print("Expected: '" + HEAD + "'.")
183 print("Actual: '" + currentHEAD + "'.")
185 if __name__ == '__main__':
186 print("Creating a new repository in " + repopath)
188 # Files
189 c = "content.txt"
190 n = "notes.txt"
191 t = "test.c"
193 # Branches
194 m = "master"
195 mt = "maint"
196 p = "pu"
198 # Start afresh
199 setupFreshRepo()
201 # Create some linear history
202 createLinearHistory(c, createFile=True, initialCommit=True, finalCommit=True)
204 # Create a maintenance branch
205 createBranch(mt, True)
207 # Create some history there too
208 createLinearHistory(n, createFile=True, count=3)
210 # Go back to master and merge with maint
211 checkoutBranch(m)
212 mergeBranch(mt)
214 # Create a playground branch and create some history
215 # This branch will be left 'dead', e.g., unused after this
216 createBranch(p)
217 createLinearHistory(t, createFile=True, count=7, finalCommit=True)
219 # Revert that commit
220 revertLastCommit()
222 # Go back to master and continue some history
223 checkoutBranch(m)
224 createLinearHistory(c, start=10, count=5)
226 # Yay, our first release!
227 tagRelease(1)
229 # Merge current master into maint
230 checkoutBranch(mt)
231 mergeBranch(m)
233 # Ouch! Brown paper bag fix there, correct it and merge into master
234 revertLastCommit(commitChanges=False)
235 createLinearHistory(c, start=42, count=1)
236 checkoutBranch(m)
237 mergeBranch(mt)
239 # Continue some work on master
240 createLinearHistory(c, start=16, count=6)
242 # Check that the current head is as expected
243 # Note: this value has to be updated whenever this script is changed.
244 checkHead("aeeaccca2e06a1824017366008ce7d8559d1c4d7")