-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_functionality.php
More file actions
84 lines (56 loc) · 1.9 KB
/
Copy pathgit_functionality.php
File metadata and controls
84 lines (56 loc) · 1.9 KB
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
<?php
/*
* The code below was taken from the Git Branches Info plugin by Konrad Karpieszuk, http://muzungu.pl.
*/
namespace TclStatus\Git;
function get_git_head_file_content( $repository_path ) {
$head_file_path = construct_head_path( $repository_path );
if ( is_file( $head_file_path ) && is_readable( $head_file_path ) ) {
$file = file_get_contents( $head_file_path );
}
return isset( $file ) ? $file : null;
}
function get_git_fetch_head_file_time( $repository_path ) {
$head_fetch_file_path = construct_fetch_head_path( $repository_path );
if (!is_file($head_fetch_file_path)) {
$time = null;
} else {
$time = filemtime($head_fetch_file_path);
}
return $time;
}
function construct_fetch_head_path( $repository_path ) {
$git_dir_name = git_directory_path( $repository_path );
$file_path = $git_dir_name . DIRECTORY_SEPARATOR . 'FETCH_HEAD';
return $file_path;
}
function construct_head_path( $repository_path ) {
$git_dir_name = git_directory_path( $repository_path );
$head_file_path = $git_dir_name . DIRECTORY_SEPARATOR . 'HEAD';
return $head_file_path;
}
function git_directory_path( $repository_path ) {
$repository_path = untrailingslashit( $repository_path );
if ( PHP_OS == "Windows" || PHP_OS == "WINNT" ) {
$repository_path = str_replace( "/", DIRECTORY_SEPARATOR, $repository_path );
}
$git_dir_name = $repository_path . DIRECTORY_SEPARATOR . ".git";
return $git_dir_name;
}
function get_branch_name($file_content) {
$lines = explode( "\n", $file_content );
$branch_name = false;
foreach ( $lines as $line ) {
if ( strpos( $line, 'ref:' ) === 0 ) {
$in_line = explode( "/", $line );
// Handle special case with feature/issue-000 branch names
if( 4 == count( $in_line ) && 'heads' == $in_line[1] ) {
$branch_name = $in_line[2] . '/' . $in_line[3];
} else {
$branch_name = $in_line[ count( $in_line ) - 1 ];
}
break;
}
}
return $branch_name;
}