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.
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
)
32 # Date taken from t/t1400-update-ref.sh
33 os
.environ
["GIT_COMMITTER_NAME"] = "C O Mitter"
34 os
.environ
["GIT_COMMITTER_EMAIL"] = "committer@example.com"
35 os
.environ
["GIT_COMMITTER_DATE"] = "2005-05-26 23:30"
36 os
.environ
["GIT_AUTHOR_NAME"] = "A U Thor"
37 os
.environ
["GIT_AUTHOR_EMAIL"] = "author@example.com"
38 os
.environ
["GIT_AUTHOR_DATE"] = "2005-05-26 23:30"
39 os
.environ
["TZ"] = "UTC"
44 def createLinearHistory(filename
, createFile
=False, initialCommit
=False, start
=1, count
=10, finalCommit
=False):
45 """Creates a linear history in the directory of the current repository.
47 The history is fairly simple and is all acted upon the specified file.
48 Any exceptions thrown during IO operations are _not_ cought.
51 filename: The name of the file in which to generate the history.
52 createFile: Whether the specified file has to be created first.
53 initialCommit: Whether to create an initial commit.
54 start: Which commit to start the history at.
55 count: The size of the history.
56 finalCommit: Whether to create a final commit.
68 # Create or open the content file
69 file = open(filename
, mode
)
71 # If it is new, add it to the repository as 'git commit -a' will not do so
76 # Start out with an initial change
77 file.write("Initial change\n")
80 # Add the file and create the initial commit
81 git
.commit("-a", "-m", "Initial commit")
83 # Create a linear history
84 for i
in range(start
, stop
):
85 file.write("Change " + str(i
) + "\n")
88 git
.commit("-a", "-m", "Commit " + str(i
))
91 # Finish it up with a final change
92 file.write("Last change\n")
96 git
.commit("-a", "-m", "Last commit")
98 def createBranch(name
, checkoutBranch
=True):
99 """Creates a branch with the specified name and optionally checks it out
102 name: The name of the new branch.
103 checkoutBranch: Whether to perform a checkout of the branch.
113 def checkoutBranch(name
):
114 """Switches to the specified branch
117 name: The name of the branch to be switched to.
124 def mergeBranch(name
):
125 """Merges the current branch with the specified branch
128 name: The name of the branch to merge with.
135 def revertLastCommit(commitChanges
=True):
136 """Reverts the last commit made
139 commitChanges: Whether to commit the revert.
144 options
= ["--no-edit", "HEAD"]
146 if not commitChanges
:
151 def tagRelease(releaseNumber
):
154 The created tag is 'v' + releaseNumber.
157 releaseNumber: The number of the release.
162 git
.tag('v' + str(releaseNumber
))
165 """Verifies that the HEAD is as expected.
168 HEAD: The expected value of HEAD.
173 result
= git
.rev_parse("HEAD")
175 # Eat the trailing newline
176 currentHEAD
= result
[:-1]
178 scriptSuccessful
= (HEAD
== currentHEAD
)
181 print("Done, repo created successfully")
184 print("Something went wrong, current HEAD doesn't match.")
185 print("Expected: '" + HEAD
+ "'.")
186 print("Actual: '" + currentHEAD
+ "'.")
190 print("Creating a new repository in " + repopath
)
205 # Create some linear history
206 createLinearHistory(c
, createFile
=True, initialCommit
=True, finalCommit
=True)
208 # Create a maintenance branch
209 createBranch(mt
, True)
211 # Create some history there too
212 createLinearHistory(n
, createFile
=True, count
=3)
214 # Go back to master and merge with maint
218 # Create a playground branch and create some history
219 # This branch will be left 'dead', e.g., unused after this
221 createLinearHistory(t
, createFile
=True, count
=7, finalCommit
=True)
226 # Go back to master and continue some history
228 createLinearHistory(c
, start
=10, count
=5)
230 # Yay, our first release!
233 # Merge current master into maint
237 # Ouch! Brown paper bag fix there, correct it and merge into master
238 revertLastCommit(commitChanges
=False)
239 createLinearHistory(c
, start
=42, count
=1)
243 # Continue some work on master
244 createLinearHistory(c
, start
=16, count
=6)
246 # Check that the current head is as expected
247 # Note: this value has to be updated whenever this script is changed.
248 result
= checkHead("90663811ccca2398df51c0b3c971ef9d3834989a")
250 # If the head was wrong, exit with an error code
256 if __name__
== '__main__':