gitstats: Made GitStats compatible with the latest version of git-python
[git-stats.git] / src / scripts / setupRepo.py
blobb614e5a91200ee0264f05e90253b738eee785162
1 #!/usr/bin/env python
3 import os
4 import tempfile
6 from git import Repo
7 from git 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)
31 os.chdir(repopath)
33 setDefaultInfo()
35 def setDefaultInfo():
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.
60 Params:
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.
67 """
69 git = Git(".")
71 stop = start + count
73 if createFile:
74 mode = "w"
75 else:
76 mode = "a"
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
82 if createFile:
83 git.add(filename)
85 if initialCommit:
86 # Start out with an initial change
87 file.write("Initial change\n")
88 file.flush()
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")
96 file.flush()
98 git.commit("-a", "-m", "Commit " + str(i))
100 if finalCommit:
101 # Finish it up with a final change
102 file.write("Last change\n")
103 file.flush()
105 # And a final commit
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
111 Params:
112 name: The name of the new branch.
113 checkoutBranch: Whether to perform a checkout of the branch.
116 git = Git(".")
118 git.branch(name)
120 if checkoutBranch:
121 git.checkout(name)
123 def checkoutBranch(name):
124 """Switches to the specified branch
126 Params:
127 name: The name of the branch to be switched to.
130 git = Git(".")
132 git.checkout(name)
134 def mergeBranch(name):
135 """Merges the current branch with the specified branch
137 Params:
138 name: The name of the branch to merge with.
141 git = Git(".")
143 git.merge(name)
145 def revertLastCommit(commitChanges=True):
146 """Reverts the last commit made
148 Params:
149 commitChanges: Whether to commit the revert.
152 git = Git(".")
154 options = ["--no-edit", "HEAD"]
156 if not commitChanges:
157 options.append("-n")
159 git.revert(*options)
161 def tagRelease(releaseNumber):
162 """Tags a release.
164 The created tag is 'v' + releaseNumber.
166 Params:
167 releaseNumber: The number of the release.
170 git = Git(".")
172 git.tag('v' + str(releaseNumber))
174 def checkHead(HEAD):
175 """Verifies that the HEAD is as expected.
177 Params:
178 HEAD: The expected value of HEAD.
181 git = Git(".")
183 result = git.rev_parse("HEAD")
185 # Eat the trailing newline
186 currentHEAD = result[:-1]
188 scriptSuccessful = (HEAD == currentHEAD)
190 if scriptSuccessful:
191 print("Done, repo created successfully")
192 return True
193 else:
194 print("Something went wrong, current HEAD doesn't match.")
195 print("Expected: '" + HEAD + "'.")
196 print("Actual: '" + currentHEAD + "'.")
197 return False
199 def main():
200 print("Creating a new repository in " + repopath)
202 # Files
203 c = "content.txt"
204 n = "notes.txt"
205 t = "test.c"
207 # Branches
208 m = "master"
209 mt = "maint"
210 p = "pu"
212 # Start afresh
213 setupFreshRepo()
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
225 checkoutBranch(m)
226 mergeBranch(mt)
228 # Create a playground branch and create some history
229 # This branch will be left 'dead', e.g., unused after this
230 createBranch(p)
231 createLinearHistory(t, createFile=True, count=7, finalCommit=True)
233 # Revert that commit
234 revertLastCommit()
236 # Go back to master and continue some history
237 checkoutBranch(m)
238 createLinearHistory(c, start=10, count=5)
240 # Yay, our first release!
241 tagRelease(1)
243 # Merge current master into maint
244 checkoutBranch(mt)
245 mergeBranch(m)
247 # Ouch! Brown paper bag fix there, correct it and merge into master
248 revertLastCommit(commitChanges=False)
249 createLinearHistory(c, start=42, count=1)
250 checkoutBranch(m)
251 mergeBranch(mt)
253 # Continue some work on master
254 createLinearHistory(c, start=16, count=6)
256 # Have someone else do some work on master
257 setSecondaryInfo()
258 createLinearHistory(c, start=22, count=2)
260 # Go back to the initial data
261 setDefaultInfo()
263 return 0
265 if __name__ == '__main__':
266 import sys
267 sys.exit(main())