gitstats: Use the same configurations as test-lib.sh in setupRepo
[git-stats.git] / src / scripts / setupRepo.py
blobb5d9b23d76a836b9aa2132efca5162b7c0bd55e8
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_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"
40 os.chdir(repopath)
42 git = Git(".")
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.
50 Params:
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.
57 """
59 git = Git(".")
61 stop = start + count
63 if createFile:
64 mode = "w"
65 else:
66 mode = "a"
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
72 if createFile:
73 git.add(filename)
75 if initialCommit:
76 # Start out with an initial change
77 file.write("Initial change\n")
78 file.flush()
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")
86 file.flush()
88 git.commit("-a", "-m", "Commit " + str(i))
90 if finalCommit:
91 # Finish it up with a final change
92 file.write("Last change\n")
93 file.flush()
95 # And a final commit
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
101 Params:
102 name: The name of the new branch.
103 checkoutBranch: Whether to perform a checkout of the branch.
106 git = Git(".")
108 git.branch(name)
110 if checkoutBranch:
111 git.checkout(name)
113 def checkoutBranch(name):
114 """Switches to the specified branch
116 Params:
117 name: The name of the branch to be switched to.
120 git = Git(".")
122 git.checkout(name)
124 def mergeBranch(name):
125 """Merges the current branch with the specified branch
127 Params:
128 name: The name of the branch to merge with.
131 git = Git(".")
133 git.merge(name)
135 def revertLastCommit(commitChanges=True):
136 """Reverts the last commit made
138 Params:
139 commitChanges: Whether to commit the revert.
142 git = Git(".")
144 options = ["--no-edit", "HEAD"]
146 if not commitChanges:
147 options.append("-n")
149 git.revert(*options)
151 def tagRelease(releaseNumber):
152 """Tags a release.
154 The created tag is 'v' + releaseNumber.
156 Params:
157 releaseNumber: The number of the release.
160 git = Git(".")
162 git.tag('v' + str(releaseNumber))
164 def checkHead(HEAD):
165 """Verifies that the HEAD is as expected.
167 Params:
168 HEAD: The expected value of HEAD.
171 git = Git(".")
173 result = git.rev_parse("HEAD")
175 # Eat the trailing newline
176 currentHEAD = result[:-1]
178 scriptSuccessful = (HEAD == currentHEAD)
180 if scriptSuccessful:
181 print("Done, repo created successfully")
182 return True
183 else:
184 print("Something went wrong, current HEAD doesn't match.")
185 print("Expected: '" + HEAD + "'.")
186 print("Actual: '" + currentHEAD + "'.")
187 return False
189 def main():
190 print("Creating a new repository in " + repopath)
192 # Files
193 c = "content.txt"
194 n = "notes.txt"
195 t = "test.c"
197 # Branches
198 m = "master"
199 mt = "maint"
200 p = "pu"
202 # Start afresh
203 setupFreshRepo()
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
215 checkoutBranch(m)
216 mergeBranch(mt)
218 # Create a playground branch and create some history
219 # This branch will be left 'dead', e.g., unused after this
220 createBranch(p)
221 createLinearHistory(t, createFile=True, count=7, finalCommit=True)
223 # Revert that commit
224 revertLastCommit()
226 # Go back to master and continue some history
227 checkoutBranch(m)
228 createLinearHistory(c, start=10, count=5)
230 # Yay, our first release!
231 tagRelease(1)
233 # Merge current master into maint
234 checkoutBranch(mt)
235 mergeBranch(m)
237 # Ouch! Brown paper bag fix there, correct it and merge into master
238 revertLastCommit(commitChanges=False)
239 createLinearHistory(c, start=42, count=1)
240 checkoutBranch(m)
241 mergeBranch(mt)
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
251 if not result:
252 return 1
253 else:
254 return 0
256 if __name__ == '__main__':
257 import sys
258 sys.exit(main())