forked from git4p/git4p
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.php
executable file
·164 lines (133 loc) · 3.75 KB
/
test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/php
<?php
/*
* This file is part of the Git4P library.
*
* Copyright (c) 2015 Martijn van der Kleijn <[email protected]>
* Licensed under the MIT license <http://opensource.org/licenses/MIT>
*/
/* QUICK TEST/EXAMPLE FILE */
include 'src/git4p.php';
use Git4p\Git;
use Git4p\GitBlob;
use Git4p\GitCommit;
use Git4p\GitTag;
use Git4p\GitTree;
use Git4p\GitUser;
// Test setup
$readme = "GIT4P\n=====\n\nThis is a simple test repo for git4p.\n";
$dir = dirname(__FILE__).'/mytestrepo';
$git = false;
$author = new GitUser();
$author->setName('Martijn')
->setEmail('[email protected]')
->setTimestamp('1374058686')
->setOffset('+0200');
$committer = new GitUser();
$committer->setName('Martijn')
->setEmail('[email protected]')
->setTimestamp('1374058686')
->setOffset('+0200');
$tagger = new GitUser();
$tagger->setName('Martijn')
->setEmail('[email protected]')
->setTimestamp('1374058686')
->setOffset('+0200');
// Create the repo if necessary or just a reference to existing repo
if (file_exists($dir.'/HEAD') === false) {
echo "Repo does not exist, creating on disk.\n";
$git = Git::init($dir);
}
else {
echo "Repo exists, creating reference object instance.\n";
$git = new Git($dir);
}
// Create a basic one file initial commit, then simulate a push to the repo
echo "Simulate that a README file was commited and pushed to master.\n";
$b = new GitBlob($git);
$b->setData($readme)
->store();
echo "Created $b\n";
$arr = ['README.md' => $b];
$t = new GitTree($git);
$t->setData($arr)
->store();
echo "Created $t\n";
$c = new GitCommit($git);
$c->setTree($t->sha())
->setMessage('Initial commit.')
->addAuthor($author)
->addCommiter($committer)
->store();
echo "Created $c\n";
$oc = $c;
$firstcommit = $c->sha();
// Make sure master head ref exists and points to commit
$git->updateBranch('master', $c->sha());
// Lets create an extra branch called 'develop'
$git->updateBranch('develop', $c->sha());
// Add a commit to develop
$b = new GitBlob($git);
$b->setData("Altered README.md file!!!\n")
->store();
echo "Created $b\n";
$arr = ['README.md' => $b];
$t = new GitTree($git);
$t->setData($arr)
->store();
echo "Created $t\n";
$author->setTimestamp('1374058776');
$committer->setTimestamp('1374058776');
$c = new GitCommit($git);
$c->setTree($t->sha())
->addParent($oc->sha())
->setMessage('Update readme.')
->addAuthor($author)
->addCommiter($committer)
->store();
echo "Created $c\n";
$p = $c->sha();
// Add a commit to develop
$b = new GitBlob($git);
$b->setData("Altered README.MD file!\n")
->store();
echo "Created $b\n";
$arr = ['README.md' => $b];
$t = new GitTree($git);
$t->setData($arr)
->store();
echo "Created $t\n";
$author->setTimestamp('1374158776');
$committer->setTimestamp('1374158776');
$c = new GitCommit($git);
$c->setTree($t->sha())
->addParent($p)
->setMessage('Correct readme.')
->addAuthor($author)
->addCommiter($committer)
->store();
echo "Created $c\n";
$author->setTimestamp('1384158776');
$committer->setTimestamp('1384158776');
$sc = new GitCommit($git);
$sc->setTree($t->sha())
->addParent($firstcommit)
->addParent($c->sha())
->setMessage('Merge develop into master.')
->addAuthor($author)
->addCommiter($committer)
->store();
echo "Created $sc\n";
// Update develop branch's pointer
$git->updateBranch('develop', $c->sha());
$git->updateBranch('master', $sc->sha());
$tagger->setTimestamp('1374058776');
$tag = new GitTag($git);
$tag->setObject($oc)
->setTag('v0.1')
->setTagger($tagger)
->setMessage('Tagging the first commit...')
->store();
echo "Created $tag\n";
// Create the tag's reference
Git::writeFile($dir.'/refs/tags/'.$tag->tag(), ''.$tag->sha()."\n");