9 repopath
= os
.path
.join(tempfile
.gettempdir(), "freshrepo")
11 class InitializationException(Exception):
12 """This exception is raised when something goes wrong during initialization.
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"
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.")
30 repo
= Repo
.create(repopath
)
36 # Date taken from t/t1400-update-ref.sh
37 os
.environ
["GIT_COMMITTER_NAME"] = "C O Mitter"
38 os
.environ
["GIT_COMMITTER_EMAIL"] = "committer@example.com"
39 os
.environ
["GIT_COMMITTER_DATE"] = "2005-05-26 23:30"
40 os
.environ
["GIT_AUTHOR_NAME"] = "A U Thor"
41 os
.environ
["GIT_AUTHOR_EMAIL"] = "author@example.com"
42 os
.environ
["GIT_AUTHOR_DATE"] = "2005-05-26 23:30"
43 os
.environ
["TZ"] = "UTC"
45 def setSecondaryInfo():
46 os
.environ
["GIT_COMMITTER_NAME"] = "Retti Mmoc"
47 os
.environ
["GIT_COMMITTER_EMAIL"] = "retti.mmoc@example.com"
48 os
.environ
["GIT_COMMITTER_DATE"] = "2005-05-26 23:30"
49 os
.environ
["GIT_AUTHOR_NAME"] = "Roh Tua"
50 os
.environ
["GIT_AUTHOR_EMAIL"] = "roh.tua@example.com"
51 os
.environ
["GIT_AUTHOR_DATE"] = "2005-05-26 23:30"
52 os
.environ
["TZ"] = "UTC"
54 def createLinearHistory(filename
, createFile
=False, initialCommit
=False, start
=1, count
=10, finalCommit
=False):
55 """Creates a linear history in the directory of the current repository.
57 The history is fairly simple and is all acted upon the specified file.
58 Any exceptions thrown during IO operations are _not_ cought.
61 filename: The name of the file in which to generate the history.
62 createFile: Whether the specified file has to be created first.
63 initialCommit: Whether to create an initial commit.
64 start: Which commit to start the history at.
65 count: The size of the history.
66 finalCommit: Whether to create a final commit.
78 # Create or open the content file
79 file = open(filename
, mode
)
81 # If it is new, add it to the repository as 'git commit -a' will not do so
86 # Start out with an initial change
87 file.write("Initial change\n")
90 # Add the file and create the initial commit
91 git
.commit("-a", "-m", "Initial commit")
93 # Create a linear history
94 for i
in range(start
, stop
):
95 file.write("Change " + str(i
) + "\n")
98 git
.commit("-a", "-m", "Commit " + str(i
))
101 # Finish it up with a final change
102 file.write("Last change\n")
106 git
.commit("-a", "-m", "Last commit")
108 def createBranch(name
, checkoutBranch
=True):
109 """Creates a branch with the specified name and optionally checks it out
112 name: The name of the new branch.
113 checkoutBranch: Whether to perform a checkout of the branch.
123 def checkoutBranch(name
):
124 """Switches to the specified branch
127 name: The name of the branch to be switched to.
134 def mergeBranch(name
):
135 """Merges the current branch with the specified branch
138 name: The name of the branch to merge with.
145 def revertLastCommit(commitChanges
=True):
146 """Reverts the last commit made
149 commitChanges: Whether to commit the revert.
154 options
= ["--no-edit", "HEAD"]
156 if not commitChanges
:
161 def tagRelease(releaseNumber
):
164 The created tag is 'v' + releaseNumber.
167 releaseNumber: The number of the release.
172 git
.tag('v' + str(releaseNumber
))
175 """Verifies that the HEAD is as expected.
178 HEAD: The expected value of HEAD.
183 result
= git
.rev_parse("HEAD")
185 # Eat the trailing newline
186 currentHEAD
= result
[:-1]
188 scriptSuccessful
= (HEAD
== currentHEAD
)
191 print("Done, repo created successfully")
194 print("Something went wrong, current HEAD doesn't match.")
195 print("Expected: '" + HEAD
+ "'.")
196 print("Actual: '" + currentHEAD
+ "'.")
200 print("Creating a new repository in " + repopath
)
215 # Create some linear history
216 createLinearHistory(c
, createFile
=True, initialCommit
=True, finalCommit
=True)
218 # Create a maintenance branch
219 createBranch(mt
, True)
221 # Create some history there too
222 createLinearHistory(n
, createFile
=True, count
=3)
224 # Go back to master and merge with maint
228 # Create a playground branch and create some history
229 # This branch will be left 'dead', e.g., unused after this
231 createLinearHistory(t
, createFile
=True, count
=7, finalCommit
=True)
236 # Go back to master and continue some history
238 createLinearHistory(c
, start
=10, count
=5)
240 # Yay, our first release!
243 # Merge current master into maint
247 # Ouch! Brown paper bag fix there, correct it and merge into master
248 revertLastCommit(commitChanges
=False)
249 createLinearHistory(c
, start
=42, count
=1)
253 # Continue some work on master
254 createLinearHistory(c
, start
=16, count
=6)
256 # Have someone else do some work on master
258 createLinearHistory(c
, start
=22, count
=2)
260 # Go back to the initial data
265 if __name__
== '__main__':