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_DATE"] = "2005-05-26 23:30"
34 os
.environ
["GIT_AUTHOR_DATE"] = "2005-05-26 23:30"
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.
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.
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
74 # Start out with an initial change
75 file.write("Initial change\n")
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")
86 git
.commit("-a", "-m", "Commit " + str(i
))
89 # Finish it up with a final change
90 file.write("Last change\n")
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
100 name: The name of the new branch.
101 checkoutBranch: Whether to perform a checkout of the branch.
111 def checkoutBranch(name
):
112 """Switches to the specified branch
115 name: The name of the branch to be switched to.
122 def mergeBranch(name
):
123 """Merges the current branch with the specified branch
126 name: The name of the branch to merge with.
133 def revertLastCommit(commitChanges
=True):
134 """Reverts the last commit made
137 commitChanges: Whether to commit the revert.
142 options
= ["--no-edit", "HEAD"]
144 if not commitChanges
:
149 def tagRelease(releaseNumber
):
152 The created tag is 'v' + releaseNumber.
155 releaseNumber: The number of the release.
160 git
.tag('v' + str(releaseNumber
))
163 """Verifies that the HEAD is as expected.
166 HEAD: The expected value of HEAD.
171 result
= git
.rev_parse("HEAD")
173 # Eat the trailing newline
174 currentHEAD
= result
[:-1]
176 scriptSuccessful
= (HEAD
== currentHEAD
)
179 print("Done, repo created successfully")
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
)
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
214 # Create a playground branch and create some history
215 # This branch will be left 'dead', e.g., unused after this
217 createLinearHistory(t
, createFile
=True, count
=7, finalCommit
=True)
222 # Go back to master and continue some history
224 createLinearHistory(c
, start
=10, count
=5)
226 # Yay, our first release!
229 # Merge current master into maint
233 # Ouch! Brown paper bag fix there, correct it and merge into master
234 revertLastCommit(commitChanges
=False)
235 createLinearHistory(c
, start
=42, count
=1)
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")