9 testPath
= os
.path
.join(tempfile
.gettempdir(), "testrepo")
10 metricsPath
= os
.path
.join(tempfile
.gettempdir(), "metricsrepo")
12 class InitializationException(Exception):
13 """This exception is raised when something goes wrong during initialization.
17 """Creates a fresh repo under the specified path
19 If the specified path exists an exception is raised.
22 path: The path where the new repo should be created at.
25 print("Creating a new repository in " + path
)
27 # Check if the path exists already, if so bail out
28 if os
.path
.exists(path
):
29 raise InitializationException(
30 "The specified path, " + path
+ ", exists already, "
31 "please remove it or change the target path.")
34 repo
= Repo
.create(path
)
38 """Configures GIT_COMMITTER_DATE etc to a default value.
40 These values are taken from t/test-lib.sh and t/t1400-update-ref.sh from
41 git.git. The actual date used is 2005-05-26 23:30
44 # Date taken from t/t1400-update-ref.sh
45 os
.environ
["GIT_COMMITTER_NAME"] = "C O Mitter"
46 os
.environ
["GIT_COMMITTER_EMAIL"] = "committer@example.com"
47 os
.environ
["GIT_COMMITTER_DATE"] = "2005-05-26 23:30"
48 os
.environ
["GIT_AUTHOR_NAME"] = "A U Thor"
49 os
.environ
["GIT_AUTHOR_EMAIL"] = "author@example.com"
50 os
.environ
["GIT_AUTHOR_DATE"] = "2005-05-26 23:30"
51 os
.environ
["TZ"] = "UTC"
53 def setSecondaryInfo():
54 """Configures GIT_COMMITTER_DATE and GIT_AUTHOR_DATE to a secondary value.
56 These values are taken from t/test-lib.sh and t/t1400-update-ref.sh from
57 git.git. The actual date used is 2005-05-26 23:30
61 os
.environ
["GIT_COMMITTER_NAME"] = "Retti Mmoc"
62 os
.environ
["GIT_COMMITTER_EMAIL"] = "retti.mmoc@example.com"
63 os
.environ
["GIT_COMMITTER_DATE"] = "2005-05-26 23:30"
64 os
.environ
["GIT_AUTHOR_NAME"] = "Roh Tua"
65 os
.environ
["GIT_AUTHOR_EMAIL"] = "roh.tua@example.com"
66 os
.environ
["GIT_AUTHOR_DATE"] = "2005-05-26 23:30"
67 os
.environ
["TZ"] = "UTC"
69 def addFile(filename
, createFile
=True, commit
=True):
70 """Adds the specified file
73 filename: The name of the file to add.
74 createFile: Whether the file should be created.
78 file = open(filename
, "w")
86 git
.commit("-m", "Added file " + filename
)
88 def deleteFile(filename
, leaveOnDisk
=False):
89 """Deletes the specified file
92 filename: The name of the file to delete.
93 leaveOnDisk: Whether the file should not be physically deleted.
98 # If the file should not be deleted, tell 'git rm' so
100 args
.append("--cached")
102 args
.append(filename
)
108 git
.commit("-m", "Deleted file " + filename
)
110 def createLinearHistory(filename
, createFile
=False, initialCommit
=False, start
=1, count
=10, finalCommit
=False):
111 """Creates a linear history in the directory of the current repository.
113 The history is fairly simple and is all acted upon the specified file.
114 Any exceptions thrown during IO operations are _not_ cought.
117 filename: The name of the file in which to generate the history.
118 createFile: Whether the specified file has to be created first.
119 initialCommit: Whether to create an initial commit.
120 start: Which commit to start the history at.
121 count: The size of the history.
122 finalCommit: Whether to create a final commit.
130 addFile(filename
, commit
=False)
132 # Create or open the content file
133 file = open(filename
, "a")
136 # Start out with an initial change
137 file.write("Initial change\n")
140 # Add the file and create the initial commit
141 git
.commit("-a", "-m", "Initial commit")
143 # Create a linear history
144 for i
in range(start
, stop
):
145 file.write("Change " + str(i
) + "\n")
148 git
.commit("-a", "-m", "Commit " + str(i
))
151 # Finish it up with a final change
152 file.write("Last change\n")
156 git
.commit("-a", "-m", "Last commit")
158 def createBranch(name
, checkout
=True):
159 """Creates a branch with the specified name and optionally checks it out
162 name: The name of the new branch.
163 checkout: Whether to perform a checkout of the branch.
173 def createDir(name
, changeDir
=False):
174 """Creates a directory with the specified name
177 name: The name of the directory
182 def checkoutBranch(name
):
183 """Switches to the specified branch
186 name: The name of the branch to be switched to.
193 def checkoutRelease(release
):
194 """Switches to the specified release
197 release: The release to check out
200 checkoutBranch('v' + str(release
))
202 def mergeBranch(name
):
203 """Merges the current branch with the specified branch
206 name: The name of the branch to merge with.
213 def revertLastCommit(commitChanges
=True):
214 """Reverts the last commit made
217 commitChanges: Whether to commit the revert.
222 options
= ["--no-edit", "HEAD"]
224 if not commitChanges
:
229 def tagRelease(releaseNumber
):
232 The created tag is 'v' + releaseNumber.
235 releaseNumber: The number of the release.
240 git
.tag('v' + str(releaseNumber
))
242 def createRemoteBranch(name
, start
="HEAD"):
243 """Creates a new remote branch with the specified name
246 name: The plain name of the remote (no '/refs/remotes/' prefix)
247 start: The revision to branch off from
252 git
.update_ref('refs/remotes/' + name
, start
)
255 """Verifies that the HEAD is as expected.
258 HEAD: The expected value of HEAD.
263 result
= git
.rev_parse("HEAD")
265 # Eat the trailing newline
266 currentHEAD
= result
[:-1]
268 scriptSuccessful
= (HEAD
== currentHEAD
)
271 print("Done, repo created successfully")
274 print("Something went wrong, current HEAD doesn't match.")
275 print("Expected: '" + HEAD
+ "'.")
276 print("Actual: '" + currentHEAD
+ "'.")
279 def createTestRepository():
280 """Creates a test repository under setupRepo.testpath
290 r
= os
.path
.join(d
, "README.txt")
302 # Set the default author/committer
305 # Create some linear history
306 createLinearHistory(c
, createFile
=True, initialCommit
=True, finalCommit
=True)
308 # Create a maintenance branch
311 # Create some history there too
312 createLinearHistory(n
, createFile
=True, count
=3)
314 # Go back to master and merge with maint
318 # Create a playground branch and create some history
319 # This branch will be left 'dead', e.g., unused after this
321 createLinearHistory(t
, createFile
=True, count
=7, finalCommit
=True)
326 # Go back to master and continue some history
328 createLinearHistory(c
, start
=10, count
=5)
330 # Yay, our first release!
333 # Merge current master into maint
337 # Ouch! Brown paper bag fix there, correct it and merge into master
338 revertLastCommit(commitChanges
=False)
339 createLinearHistory(c
, start
=42, count
=1)
343 # Continue some work on master
344 createLinearHistory(c
, start
=16, count
=6)
346 # Have someone else do some work on master
348 createLinearHistory(c
, start
=22, count
=2)
350 # Go back to the initial data
353 # Create a directory for the documentation
356 # Create a readme and add some content to it
357 createLinearHistory(r
, createFile
=True, count
=5, finalCommit
=True)
359 # Ah, but that last one was bad, we don't want it
362 # Instead, continue the linear history
363 createLinearHistory(r
, start
=6, count
=1)
365 # Come to think of it, that -was- a really good change after all
368 # Reinstate that final commit
369 createLinearHistory(r
, count
=0, finalCommit
=True)
371 # Create a branch to delete a file on
374 # Remove a file from git
375 deleteFile(r
, leaveOnDisk
=True)
377 # Only to add it back later so that we don't have any dangling files
378 addFile(r
, createFile
=False)
380 # Create a release at this point for easy switching later
383 # Switch back to master for the test suite
386 # Create a remote branch
387 createRemoteBranch(j
, start
="HEAD^")
391 def createMetricsRepository():
392 """Creates a metrics repository in setupRepo.metricspath
405 # Create a repository to work in
406 setupRepo(metricsPath
)
411 # And create some history
412 createLinearHistory(c
, createFile
=True, count
=2)
414 # Mark this commit so that we can jump back to it later
417 # And create some more history
418 createLinearHistory(c
, start
=3, count
=3)
420 # Go back to that marked commit
423 # Now create a new branch to work on
426 # Fix us up some more history
427 createLinearHistory(n
, createFile
=True, count
=2)
429 # Mark this commit too so we can jump back
432 # And make some more history
433 createLinearHistory(n
, start
=2, count
=1)
435 # Now merge this back into master
439 # Go back to maint to create some more there
441 createLinearHistory(n
, start
=3, count
=1)
443 # Now let's go back and fix that one commit
446 createLinearHistory(r
, createFile
=True, count
=1)
448 # Go back to maint, and merge it in
452 # Now we can create the last bit of history here
453 createLinearHistory(n
, start
=4, count
=3)
455 # Now we can create our last bit of history on master
457 createLinearHistory(c
, start
=6, count
=3)
462 """Creates a test and a metrics repository
465 args: An array of arguments, when 2 in size the second
466 element should either be 'test' to just create the test
467 repo, or 'metrics' to just create the metrics
468 repository. If both should be created then the size of
469 args should be 1. The first element in the array is
472 Returns: 0 upon success, or nonzero upon failure.
477 if size
> 1 and (args
[1] != "test" and args
[1] != "metrics") or size
> 2:
478 print "Please specify either 'test', 'metrics', or nothing to run both"
481 if len(args
) == 1 or args
[1] == "test":
482 ret
= createTestRepository()
486 if len(args
) == 1 or args
[1] == "metrics":
487 ret
= createMetricsRepository()
493 if __name__
== '__main__':