Permalink
Browse files

initial commit - adds p4 scripts to repo

  • Loading branch information...
0 parents commit f29fc524ccb302ef84070eb300593c088ed87bfe @bshaffer committed Jan 5, 2012
Showing with 329 additions and 0 deletions.
  1. +113 −0 p4-diff
  2. +3 −0 p4-new
  3. +77 −0 p4-open
  4. +65 −0 p4-revert
  5. +53 −0 p4-submit
  6. +18 −0 p4-sync
113 p4-diff
@@ -0,0 +1,113 @@
+#!/usr/local/bin/php
+<?php
+
+echo "\n";
+$args = array_merge(array(
+ 'p' => false,
+ 'show' => 'all',
+), parse_args($argv));
+
+$path = isset($args[0]) ? $args[0] : './...';
+
+$list = '';
+if ($args['show'] == 'open' || $args['show'] == 'all') {
+ $list .= `p4 diff -sa $path`;
+}
+if ($args['show'] == 'unopen' || $args['show'] == 'all') {
+ $list .= `p4 diff -se $path`;
+}
+
+if(strlen($list) < 1) {
+ die();
+}
+
+$lines = explode("\n", trim($list));
+
+$diff = '';
+foreach($lines as $line) {
+ $fileDiff = `p4 diff -du -f $line`;
+ $diffLines = explode("\n", $fileDiff);
+ $oldFile = substr($diffLines[0], 4, strpos($diffLines[0], "\t") - 4);
+ $newFile = substr($diffLines[1], 4, strpos($diffLines[1], "\t") - 4);
+ $common = strrev(longest_common_prefix(array(strrev($oldFile), strrev($newFile))));
+ $common = str_replace('/'.$branch.'/', '/', $common);
+ $common = str_replace('/'.$repo.'/', '/', $common);
+ $common = $common[0] == '/' ? substr($common, 1) : $common;
+ $fileDiff = str_replace($oldFile, $common, str_replace($newFile, $common, $fileDiff));
+ $diff .= $fileDiff . "\n";
+}
+
+if (!$args['p']) {
+ $red = "\033[31m";
+ $green = "\033[32m";
+ $blue = "\033[36m";
+ $yellow = "\033[33m";
+ $normal = "\033[0m";
+
+ $diff = preg_replace('/(@@.*@@)/', "$blue$1$normal", $diff);
+ $diff = preg_replace('/^(\+[^+].*)/m', "$green$1$normal", $diff);
+ $diff = preg_replace('/^(\-[^-].*)/m', "$red$1$normal", $diff);
+ $diff = preg_replace('/^(\-\-\-.*)/m', "$red$1$normal", $diff);
+ $diff = preg_replace('/^(\+\+\+.*)/m', "$green$1$normal", $diff);
+}
+
+die($diff);
+
+// ================
+// HELPER FUNCTIONS
+// ================
+
+function parse_args($argv){
+ array_shift($argv);
+ $out = array();
+ foreach ($argv as $arg){
+ if (substr($arg,0,2) == '--'){
+ $eqPos = strpos($arg,'=');
+ if ($eqPos === false){
+ $key = substr($arg,2);
+ $out[$key] = isset($out[$key]) ? $out[$key] : true;
+ } else {
+ $key = substr($arg,2,$eqPos-2);
+ $out[$key] = substr($arg,$eqPos+1);
+ }
+ } else if (substr($arg,0,1) == '-'){
+ if (substr($arg,2,1) == '='){
+ $key = substr($arg,1,1);
+ $out[$key] = substr($arg,3);
+ } else {
+ $chars = str_split(substr($arg,1));
+ foreach ($chars as $char){
+ $key = $char;
+ $out[$key] = isset($out[$key]) ? $out[$key] : true;
+ }
+ }
+ } else {
+ $out[] = $arg;
+ }
+ }
+ return $out;
+}
+
+function longest_common_prefix(array $arr, $ignore_empty = false)
+{
+ $prefix = NULL;
+
+ foreach($arr as $s)
+ {
+ if($ignore_empty && empty($s)) continue;
+
+ if(is_null($prefix))
+ {
+ $prefix = $s;
+ }
+ else
+ {
+ while(!empty($prefix) && substr($s, 0, strlen($prefix)) != $prefix)
+ {
+ $prefix = substr($prefix, 0, -1);
+ }
+ }
+ }
+
+ return $prefix;
+}
3 p4-new
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+find $(pwd -P) -type f ! -name '*~' -print0| xargs -0 p4 fstat 2>&1|awk '/no such file/{print $1}'
77 p4-open
@@ -0,0 +1,77 @@
+#!/usr/local/bin/php
+<?php
+
+echo "\n";
+$path = './...';
+
+$args = array_merge(array(
+ 'no-confirmation' => false,
+ ), parseArgs($argv));
+
+if (isset($args[0])) {
+ $path = $args[0];
+}
+else if (preg_match('/\/home\/httpd\/(\w*)\/devzone.*\/(\w*)\/(\w*)/', getcwd(), $matches)) {
+ $user = $matches[1];
+ $repo = $matches[2];
+ $branch = $matches[3];
+ $path = $branch == 'trunk' ? sprintf('//depot/%s/trunk/...', $repo) : sprintf('//depot/%s/branches/%s/...', $repo, $branch);
+}
+
+$files = array_filter(explode("\n", `p4 diff -se $path`));
+
+if (count($files) == 0) {
+ die("no files to open");
+}
+
+if (!$args['no-confirmation']) {
+ $f = fopen('php://stdin', 'r');
+ echo "Files to open: \n\t";
+ echo implode("\n\t", $files) . "\n";
+ echo "OK to add? (y/n): ";
+ if (trim(fgets($f)) != 'y') {
+ die("Aborted");
+ }
+}
+$filesList = implode(' ', $files);
+$ret = `p4 edit $filesList`;
+die($ret);
+
+
+// ================
+// HELPER FUNCTIONS
+// ================
+
+function parseArgs($argv){
+ array_shift($argv);
+ $out = array();
+ foreach ($argv as $arg){
+ if (substr($arg,0,2) == '--'){
+ $eqPos = strpos($arg,'=');
+ if ($eqPos === false){
+ $key = substr($arg,2);
+ $out[$key] = isset($out[$key]) ? $out[$key] : true;
+ } else {
+ $key = substr($arg,2,$eqPos-2);
+ $out[$key] = substr($arg,$eqPos+1);
+ }
+ } else if (substr($arg,0,1) == '-'){
+ if (substr($arg,2,1) == '='){
+ $key = substr($arg,1,1);
+ $out[$key] = substr($arg,3);
+ } else {
+ $chars = str_split(substr($arg,1));
+ foreach ($chars as $char){
+ $key = $char;
+ $out[$key] = isset($out[$key]) ? $out[$key] : true;
+ }
+ }
+ } else {
+ $out[] = $arg;
+ }
+ }
+ return $out;
+}
+
+
+
@@ -0,0 +1,65 @@
+#!/usr/local/bin/php
+<?php
+
+echo "\n";
+$path = './...';
+
+$args = array_merge(array(
+ // no args yet
+), parseArgs($argv));
+
+$fileList = array();
+
+foreach ($args as $key => $arg) {
+ if (is_numeric($key)) {
+ $fileList[] = $arg;
+ }
+}
+
+if (count($fileList) == 0) {
+ die('Provide at least one file argument to the revert command');
+}
+
+$files = implode(' ', $fileList);
+
+$ret = `p4 open $files && p4 revert $files`;
+die($ret);
+
+
+// ================
+// HELPER FUNCTIONS
+// ================
+
+function parseArgs($argv){
+ array_shift($argv);
+ $out = array();
+ foreach ($argv as $arg){
+ if (substr($arg,0,2) == '--'){
+ $eqPos = strpos($arg,'=');
+ if ($eqPos === false){
+ $key = substr($arg,2);
+ $out[$key] = isset($out[$key]) ? $out[$key] : true;
+ } else {
+ $key = substr($arg,2,$eqPos-2);
+ $out[$key] = substr($arg,$eqPos+1);
+ }
+ } else if (substr($arg,0,1) == '-'){
+ if (substr($arg,2,1) == '='){
+ $key = substr($arg,1,1);
+ $out[$key] = substr($arg,3);
+ } else {
+ $chars = str_split(substr($arg,1));
+ foreach ($chars as $char){
+ $key = $char;
+ $out[$key] = isset($out[$key]) ? $out[$key] : true;
+ }
+ }
+ } else {
+ $out[] = $arg;
+ }
+ }
+ return $out;
+}
+
+
+
@@ -0,0 +1,53 @@
+#!/usr/local/bin/php
+<?php
+
+echo "\n";
+
+$options = array_merge(array(
+ 'bug' => false,
+ 'm' => '',
+ ), parseArgs($argv));
+
+$message = sprintf("\tBugId: %s\n\t%s", $options['bug'], $options['m']);
+
+$ret = `p4 submit -d "$message"`;
+
+echo "changeset submitted!\n$ret";
+
+// ================
+// HELPER FUNCTIONS
+// ================
+
+function parseArgs($argv){
+ array_shift($argv);
+ $out = array();
+ foreach ($argv as $arg){
+ if (substr($arg,0,2) == '--'){
+ $eqPos = strpos($arg,'=');
+ if ($eqPos === false){
+ $key = substr($arg,2);
+ $out[$key] = isset($out[$key]) ? $out[$key] : true;
+ } else {
+ $key = substr($arg,2,$eqPos-2);
+ $out[$key] = substr($arg,$eqPos+1);
+ }
+ } else if (substr($arg,0,1) == '-'){
+ if (substr($arg,2,1) == '='){
+ $key = substr($arg,1,1);
+ $out[$key] = substr($arg,3);
+ } else {
+ $chars = str_split(substr($arg,1));
+ foreach ($chars as $char){
+ $key = $char;
+ $out[$key] = isset($out[$key]) ? $out[$key] : true;
+ }
+ }
+ } else {
+ $out[] = $arg;
+ }
+ }
+ return $out;
+}
+
+
+
18 p4-sync
@@ -0,0 +1,18 @@
+#!/usr/local/bin/php
+<?php
+
+if (count($argv) < 2) {
+ exit('p4-sync repository [branch or "trunk"]');
+}
+
+$repo = $argv[1];
+$cmd = 'p4 sync //depot/%s/...';
+if (count($argv) > 2) {
+ $path = $argv[2];
+ $type = $path == 'trunk' ? '' : 'branches/';
+ $repoPath = sprintf('%s/%s%s', $repo, $type, $path);
+} else {
+ $repoPath = $repo;
+}
+
+passthru(sprintf($cmd, $repoPath));

0 comments on commit f29fc52

Please sign in to comment.