first work on Fork origin display; works for local forks
authorChristian Weiske <cweiske@cweiske.de>
Wed, 21 Nov 2012 21:34:24 +0000 (22:34 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 21 Nov 2012 21:34:24 +0000 (22:34 +0100)
data/templates/display-sidebar-fork.htm [new file with mode: 0644]
data/templates/display.htm
src/phorkie/Repository.php
src/phorkie/Repository/ConnectionInfo.php [new file with mode: 0644]
src/phorkie/Repository/Remote.php [new file with mode: 0644]

diff --git a/data/templates/display-sidebar-fork.htm b/data/templates/display-sidebar-fork.htm
new file mode 100644 (file)
index 0000000..d3793e1
--- /dev/null
@@ -0,0 +1,14 @@
+{% set conns = repo.getConnectionInfo() %}
+{% if conns.isFork() %}
+ {% set origin = conns.getOrigin() %}
+ <h4>Fork of</h4>
+ <p>
+  {% set webpage = origin.getWebURL() %}
+  {% if webpage %}
+   <a href="{{webpage}}">{{origin.getTitle()}}</a>
+  {% else %}
+   {{origin.getTitle()}}
+  {% endif %}
+  (<a href="{{origin.getCloneUrl()}}">clone URL</a>)
+ </p>
+{% endif %}
\ No newline at end of file
index 2bd0d73..27d1314 100644 (file)
@@ -23,5 +23,6 @@
 
 {% block sidebar %}
  {% include 'display-sidebar-owner.htm' %}
+ {% include 'display-sidebar-fork.htm' %}
  {% include 'display-sidebar-history.htm' %}
 {% endblock %}
index 5c68ee4..6b7435c 100644 (file)
@@ -359,6 +359,14 @@ class Repository
 
         return $arCommits;
     }
+
+    /**
+     * @return Repository_ConnectionInfo
+     */
+    public function getConnectionInfo()
+    {
+        return new Repository_ConnectionInfo($this);
+    }
 }
 
 ?>
diff --git a/src/phorkie/Repository/ConnectionInfo.php b/src/phorkie/Repository/ConnectionInfo.php
new file mode 100644 (file)
index 0000000..3815856
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+namespace phorkie;
+
+class Repository_ConnectionInfo
+{
+    protected $arConfig;
+    protected $repo;
+
+
+    public function __construct(Repository $repo)
+    {
+        $this->repo = $repo;
+        $this->arConfig = parse_ini_file($this->repo->gitDir . '/config', true);
+    }
+
+    public function isFork()
+    {
+        return $this->getOrigin() !== null;
+    }
+
+
+    public function getOrigin()
+    {
+        return $this->getRemote('origin');
+    }
+
+    /**
+     * @return Repository_Remote|null NULL if the remote does not exist, array
+     *                                with repository information otherwise
+     */
+    public function getRemote($name)
+    {
+        if (!isset($this->arConfig['remote ' . $name])) {
+            return null;
+        }
+        return new Repository_Remote($name, $this->arConfig['remote ' . $name]);
+    }
+
+}
+
+
+?>
diff --git a/src/phorkie/Repository/Remote.php b/src/phorkie/Repository/Remote.php
new file mode 100644 (file)
index 0000000..4f5034c
--- /dev/null
@@ -0,0 +1,94 @@
+<?php
+namespace phorkie;
+
+class Repository_Remote
+{
+    protected $arConfig;
+    protected $name;
+
+    public function __construct($name, $arConfig)
+    {
+        $this->name = $name;
+        $this->arConfig = $arConfig;
+    }
+
+
+    public function getTitle()
+    {
+        if (isset($this->arConfig['title'])) {
+            return $this->arConfig['title'];
+        }
+        if ($this->isLocal()) {
+            $local = $this->getLocalRepository();
+            if ($local !== null) {
+                return $local->getTitle();
+            }
+            return 'deleted local paste';
+        }
+
+        return 'untitled repository';
+    }
+
+    public function getCloneURL()
+    {
+        if ($this->isLocal()) {
+            $local = $this->getLocalRepository();
+            if ($local !== null) {
+                return $local->getCloneURL();
+            }
+        }
+
+        return $this->arConfig['url'];
+    }
+
+    public function getWebURL()
+    {
+        if (isset($this->arConfig['homepage'])) {
+            return $this->arConfig['homepage'];
+        }
+
+        if ($this->isLocal()) {
+            $local = $this->getLocalRepository();
+            if ($local !== null) {
+                return $local->getLink('display');
+            }
+        }
+
+        return null;
+    }
+
+    /**
+     * Tells you if this remote repository is a paste on the local server
+     *
+     * @return boolean True of false
+     */
+    public function isLocal()
+    {
+        return isset($this->arConfig['url'])
+            && $this->arConfig['url']{0} == '/';
+    }
+
+    /**
+     * If this remote is a local paste, then we'll get the repository object
+     * returned
+     *
+     * @return Repository Repository object or NULL
+     */
+    public function getLocalRepository()
+    {
+        if (!file_exists($this->arConfig['url'] . '/config')) {
+            return null;
+        }
+        $dir = basename($this->arConfig['url']);
+        if (substr($dir, -4) != '.git') {
+            //phorks are bare repositories "123.git"
+            return null;
+        }
+        $repo = new Repository();
+        $repo->loadById(substr($dir, 0, -4));
+        return $repo;
+    }
+
+}
+
+?>