From 3d66db04aee6c2e2a82186bf44df81c7d23bf368 Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Mon, 28 Jul 2014 07:00:56 +0100 Subject: [PATCH] Working with GitHub diff mode Now an HTML type file, had no doctype, header etc previously. Now does and also loads in 2 x JS files for GitHub API control Gets full list of files, considers .gitignore files and gets an excluded list Always has a GET location now, so no need to consider If in GitHub diff mode, for each file in our list get file contents and put the name, SHA and type into 3 arrays Work out GitHub repo path and set up JS arrays the same as PHP arrays Then start a github object and begin examining tree for differences recursively, adding to arrays and removing DOM elems from branch DIV as needed With that done, we need to cover the dirs not yet opened and if they contain no changes, remove them Finally, remove .gitignore excluded filrs showFile function set up mostly as per previous code, but now also adds files that are deleted (ie, to make them show up in the tree) Error catching and helpful messages and redirections added such as when you have no diffs or used up your GitHub API rate limit --- lib/get-branch.php | 288 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 277 insertions(+), 11 deletions(-) diff --git a/lib/get-branch.php b/lib/get-branch.php index 57733d3..c64bafb 100644 --- a/lib/get-branch.php +++ b/lib/get-branch.php @@ -8,13 +8,107 @@ if (!$_SESSION['loggedIn']) { header("Location: ../"); } +?> + + + +ICEcoder v <?php echo $ICEcoder["versionNo"];?> get branch + + + + + + + +getPathname(), $b->getPathname()); +} + +// Class to put forward the values for sorting +class SortingIterator implements IteratorAggregate { + private $iterator = null; + public function __construct(Traversable $iterator, $callback) { + $array = iterator_to_array($iterator); + usort($array, $callback); + $this->iterator = new ArrayIterator($array); + } + public function getIterator() { + return $this->iterator; + } +} + +// Get a full list of dirs & files and begin sorting using above class & function +$path = $docRoot.$iceRoot; +$objectList = new SortingIterator(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST), 'alphasort'); + +// Iterator to get files +$iter = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::SELF_FIRST, + RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied" +); + +// Check if dir has .gitignore file +function hasGitignore($dir) { + return is_file("$dir/.gitignore"); +} + +// Get a list of .gitignore files into $gi array +$gi = array(); +if(hasGitignore($path)) { + $gi[] = "$path/.gitignore"; +} +foreach ($iter as $scanpath) { + if (is_dir($scanpath) && strpos($scanpath,".git") == false) { + $thisDir = str_replace("\\","/",$scanpath); + if(hasGitignore($thisDir)) { + $gi[] = $thisDir."/.gitignore"; + } + } +} + +// Get $matches array containing existing files listed in .gitignore +function parseGitignore($file) { # $file = '/absolute/path/to/.gitignore' + $dir = dirname($file); + $matches = array(); + $lines = file($file); + foreach ($lines as $line) { + $line = trim($line); + if ($line === '') continue; # empty line + if (substr($line, 0, 1) == '#') continue; # a comment + if (substr($line, 0, 1) == '!') { # negated glob + $line = substr($line, 1); + $files = array_diff(glob("$dir/*"), glob("$dir/$line")); + } else { # normal glob + $files = glob("$dir/$line"); + } + $matches = array_merge($matches, $files); + } + return $matches; +} + +// Cycle through all .gitignore files running above function to get a list of $excluded files +$excluded = array(); +foreach ($gi as $scanpath) { + $excludedTest = (parseGitignore($scanpath)); + if (count($excludedTest) > 0) { + $excluded = array_merge($excluded, $excludedTest); + } +} + +$objectListArray = array(); +foreach ($objectList as $objectRef) { + $fileFolderName = @ltrim(substr(str_replace("\\","/",$objectRef->getPathname()), strlen($path)),"/"); + array_push($objectListArray,$fileFolderName); +} + // If we're just getting a branch, get that and set as the finalArray $scanDir = $docRoot.$iceRoot; $location = ""; -if (isset($_GET['location'])) { - echo ' - \ No newline at end of file + + \ No newline at end of file