Initial commit
Benjamin Renard

Benjamin Renard commited on 2013-08-21 14:38:18
Showing 6 changed files, with 303 additions and 0 deletions.

... ...
@@ -0,0 +1,42 @@
1
+AddRepo plugin for ViewGit
2
+==========================
3
+
4
+This plugin add an interface to create new repositry directly from
5
+ViewGit : A form permit to give
6
+
7
+* repository name
8
+* repository description
9
+* a password witch protect this feature
10
+
11
+The repository name will be validated : it must contain only letter, number,
12
+"_" or "-" caracter.
13
+
14
+The repository description is required. This description will be put in the
15
+description file of the repository.
16
+
17
+The given password will be compare with $conf['addrepo']['password'] value.
18
+If password is ommit in the configuration, the creation will be refused.
19
+
20
+After create a new repository, if you go on the repository page, you will have
21
+a message that explain you how to push your first commits. This message could
22
+be change by editing template file summary.php. This message is displayed only
23
+if the head hash is empty.
24
+
25
+Prerequisites
26
+-------------
27
+
28
+* User witch running apache must have right access to the directory 
29
+  witch contains your repositories
30
+* You must have dynamic viewgit projects detection
31
+
32
+Install
33
+-------
34
+
35
+* Put addrepo directory in plugins directory
36
+* Add this block in inc/localconfig.php :
37
+
38
+$conf['addrepo'] = array(
39
+        'root_path' => '/full/path/to/your/repos',
40
+        'password' => 'secret',
41
+        'url_format' => 'git@git.example.com:public_git/%s'
42
+);
... ...
@@ -0,0 +1,158 @@
1
+<?php
2
+
3
+class AddRepoPlugin extends VGPlugin
4
+{
5
+	function __construct() {
6
+		global $conf;
7
+		if (isset($conf['addrepo'])) {
8
+			$this->register_action('addrepo');
9
+			$this->register_hook('header');
10
+			$this->register_hook('page_start');
11
+			$this->register_hook('summary');
12
+		}
13
+	}
14
+
15
+	function action($action) {
16
+		if ($action === 'addrepo') {
17
+			global $page;
18
+			global $conf;
19
+			if (isset($_POST['repo_name']) && isset($_POST['repo_desc']) && isset($_POST['repo_pwd'])) {
20
+				$create=True;
21
+				$name=rtrim($_POST['repo_name']);
22
+				if (!$this -> validate_repo_name($name)) {
23
+					$this -> add_display_message('Invalid name of repository. Must only contain letters, numbers or caraters "_" or "-". ',True);
24
+					$create=False;
25
+				}
26
+				$desc=rtrim($_POST['repo_desc']);
27
+				if (empty($desc)) {
28
+					$this -> add_display_message('Repository description is required.',True);
29
+					$create=False;
30
+				}
31
+
32
+				if (!isset($conf['addrepo']['password']) || empty($conf['addrepo']['password'])) {
33
+					$this -> add_display_message('Password not defined in configuration. Creation disable.',True);
34
+					$create=False;
35
+				}
36
+				else {
37
+					$pwd=rtrim($_POST['repo_pwd']);
38
+					if ($pwd!=$conf['addrepo']['password']) {
39
+						$this -> add_display_message('Password incorrect',True);
40
+						$create=False;
41
+					}
42
+				}
43
+
44
+				if ($create && $this -> create_repo($name,$desc)) {
45
+					$this -> add_display_message("Repository $name successfully created !<br>To access to your new repository, click <a href='?a=summary&p=$name'>here</a>.");
46
+				}
47
+				else {
48
+					$page['addrepo']['values']['name']=$name;
49
+					$page['addrepo']['values']['desc']=$desc;
50
+				}
51
+			}
52
+			$this->display_plugin_template('addrepo');
53
+		}
54
+	}
55
+
56
+	function hook($type) {
57
+		if ($type == 'header') {
58
+			echo "\t<link rel='stylesheet' href='plugins/addrepo/style.css' type='text/css' />\n";
59
+		}
60
+		elseif ($type == 'page_start') {
61
+			$this->display_plugin_template('start',False);
62
+		}
63
+		elseif ($type == 'summary') {
64
+			global $page;
65
+			global $conf;
66
+			$infos=get_project_info($page['project']);
67
+			if (empty($infos['head_hash'])) {
68
+				if (isset($conf['addrepo']['url_format'])) {
69
+					$page['addrepo']['URL']=sprintf($conf['addrepo']['url_format'],$page['project']);
70
+				}
71
+				else {
72
+					$page['addrepo']['URL']='[URL]';
73
+				}
74
+				$this->display_plugin_template('summary',False);
75
+			}
76
+		}
77
+	}
78
+
79
+        /**
80
+         * Display the given template.
81
+         * Fixe global variables inclusion
82
+         */
83
+        function display_template($template, $with_headers = true) {
84
+                global $conf;
85
+                global $page;
86
+                if ($with_headers) {
87
+                        require 'templates/header.php';
88
+                }
89
+                require "$template";
90
+                if ($with_headers) {
91
+                        require 'templates/footer.php';
92
+                }
93
+        }
94
+
95
+	function add_display_message($msg,$error=false) {
96
+		global $page;
97
+		if ($error) {
98
+			$page['addrepo']['error'][]=$msg;
99
+		}
100
+		else {
101
+			$page['addrepo']['msg'][]=$msg;
102
+		}
103
+	}
104
+
105
+	function validate_repo_name($name) {
106
+		if (preg_match('/^[a-zA-Z0-9\-\_]*$/',$name)) {
107
+			return True;
108
+		}
109
+		return False;
110
+	}
111
+
112
+	function create_repo($name,$desc) {
113
+		global $conf;
114
+		if (!isset($conf['addrepo']['root_path'])) {
115
+			$this -> add_display_message('Root path not configured !',True);
116
+			return;
117
+		}
118
+		elseif(!is_dir($conf['addrepo']['root_path'])) {
119
+			 $this -> add_display_message('Root directory not found. Check your configuration !',True);
120
+			 return;
121
+		}
122
+		$path=$conf['addrepo']['root_path'].'/'.$name;
123
+		if (is_dir($path)) {
124
+			$this -> add_display_message("Repository $name still exist !",True);
125
+			return;
126
+		}
127
+
128
+		if (!mkdir($path)) {
129
+			$this -> add_display_message("Fail to create repository's directory. Check permission.",True);
130
+			return;
131
+		}
132
+
133
+		global $conf;
134
+		$conf['project'][$name]=array(
135
+			'repo'  => $path
136
+		);
137
+
138
+		$cmd = $conf['git'] ." --git-dir=$path init";
139
+		exec($cmd,$out,$ret);
140
+		if ($ret !== 0) {
141
+			$this -> add_display_message("Error initializing repository !");
142
+			return;
143
+		}
144
+
145
+		try {
146
+			$fd = fopen($path.'/description','w');
147
+			fwrite($fd,$desc);
148
+			fclose($fd);
149
+		}
150
+		catch (Exception $e) {
151
+			 $this -> add_display_message("Error setting repository's description : ".$e->getMessage());
152
+			 return;
153
+		}
154
+		return True;
155
+	}
156
+
157
+}
158
+
... ...
@@ -0,0 +1,70 @@
1
+h1.addrepo {
2
+	background-color: #cfcfcf;
3
+	border-bottom: 1px solid #666;
4
+	border-top: 1px solid #666;
5
+	padding: 6px 2px;
6
+}
7
+
8
+div.addrepo_error, div.addrepo_msg {
9
+	width:			28em;
10
+	padding:		0.5em;
11
+	margin:			0.5em;
12
+	text-align:		center;
13
+}
14
+
15
+div.addrepo_error {
16
+	color:			#fff;
17
+	background-color:	#cb4545;
18
+	border:			1px solid #f00;
19
+}
20
+
21
+div.addrepo_msg {
22
+	color:			#fff;
23
+	background-color:	#5dc85d;
24
+	border:                 1px solid #426f3a;
25
+}
26
+dl.addrepo {
27
+	margin:		0;
28
+	padding:	0;
29
+}
30
+
31
+dl.addrepo dt {
32
+	position:		relative;
33
+	width:			8em;
34
+	left:			0.2em;
35
+	font-weight:		bold;
36
+}
37
+
38
+dl.addrepo dd {
39
+	margin-left:		9em;
40
+	margin-top:		-1.3em;
41
+	padding:		0;
42
+}
43
+
44
+dl.addrepo dd input {
45
+	width:			20em;
46
+}
47
+
48
+#addrepo_submit {
49
+	background-color:	#cfcfcf;
50
+	border:			1px solid #666;
51
+	padding:		6px;
52
+	margin-top:		1em;
53
+	margin-left:		12em;
54
+}
55
+
56
+#addrepo_btn {
57
+	float:			left;
58
+	padding:		0.5em;
59
+	background:		#cfcfcf;
60
+	-webkit-border-bottom-right-radius: 1em;
61
+	-moz-border-radius-bottomright: 1em;
62
+	border-bottom-right-radius: 1em;
63
+}
64
+
65
+#addrepo_btn a {
66
+	text-decoration:	none;
67
+	font-weight:		bold;
68
+	font-size:		1.3em;
69
+}
70
+
... ...
@@ -0,0 +1,25 @@
1
+<h1 class='addrepo'>Add repository</h1>
2
+<?php
3
+if (isset($page['addrepo']['error'])) {
4
+	foreach($page['addrepo']['error'] as $e) {
5
+		echo "<div class='addrepo_error'>$e</div>\n";
6
+	}
7
+}
8
+if (isset($page['addrepo']['msg'])) {
9
+	foreach($page['addrepo']['msg'] as $msg) {
10
+		echo "<div class='addrepo_msg'>$msg</div>\n";
11
+	}
12
+}
13
+?>
14
+
15
+<form action="?a=addrepo" method='POST'>
16
+<dl class='addrepo'>
17
+	<dt>Name</dt>
18
+	<dd><input type='text' name='repo_name' class='addrepo' value="<?php echo (isset($page['addrepo']['values']['name'])?$page['addrepo']['values']['name']:''); ?>"/></dd>
19
+	<dt>Description</dt>
20
+	<dd><input type='text' name='repo_desc' class='addrepo' value="<?php echo (isset($page['addrepo']['values']['desc'])?$page['addrepo']['values']['desc']:''); ?>"/></dd>
21
+	<dt>Password</dt>
22
+	<dd><input type='password' name='repo_pwd' class='addrepo'/></dd>
23
+</dl>
24
+<input type='submit' value='Create' id='addrepo_submit'/>
25
+</form>
... ...
@@ -0,0 +1 @@
1
+<div id="addrepo_btn"><a href='?a=addrepo'>+</a></div>
... ...
@@ -0,0 +1,7 @@
1
+<h1 class='addrepo_empty'>To push your first commits</h1>
2
+<p class='addrepo_empty'>
3
+In your local repositry :
4
+<pre>git remote add origin "<?php echo $page['addrepo']['URL']; ?>"
5
+git branch --set-upstream master origin/master
6
+git push</pre>
7
+</p>
0 8