Version is a library that helps with managing the version number of Git-hosted PHP projects

Overview

Version

Version is a library that helps with managing the version number of Git-hosted PHP projects.

Installation

You can add this library as a local, per-project dependency to your project using Composer:

composer require sebastian/version

If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:

composer require --dev sebastian/version

Usage

The constructor of the SebastianBergmann\Version class expects two parameters:

  • $release is the version number of the latest release (X.Y.Z, for instance) or the name of the release series (X.Y) when no release has been made from that branch / for that release series yet.
  • $path is the path to the directory (or a subdirectory thereof) where the sourcecode of the project can be found. Simply passing __DIR__ here usually suffices.

Apart from the constructor, the SebastianBergmann\Version class has a single public method: getVersion().

Here is a contrived example that shows the basic usage:

 declare(strict_types=1);
use SebastianBergmann\Version;

$version = new Version('1.0.0', __DIR__);

var_dump($version->getVersion());
string(18) "1.0.0-17-g00f3408"

When a new release is prepared, the string that is passed to the constructor as the first argument needs to be updated.

How SebastianBergmann\Version::getVersion() works

  • If $path is not (part of) a Git repository and $release is in X.Y.Z format then $release is returned as-is.
  • If $path is not (part of) a Git repository and $release is in X.Y format then $release is returned suffixed with -dev.
  • If $path is (part of) a Git repository and $release is in X.Y.Z format then the output of git describe --tags is returned as-is.
  • If $path is (part of) a Git repository and $release is in X.Y format then a string is returned that begins with X.Y and ends with information from git describe --tags.
Comments
  • PHPUnit runner showing wrong version number

    PHPUnit runner showing wrong version number

    Using PHPUnit as a dev requirement in a project (which uses git for version control) and running it using vendor/bin/phpunit will show the most recent tagged version of project instead of the phpunit version.

    This can be tracked down to SebastianBergmann\Version::isGitRepository. As the isGitRespository method is recursive to check till the root if there is some .git folder it can show any tag of a parent git repository instead of a phpunit tagged version.

            /**
             * @param  string $path
             * @return boolean
             */
            private function isGitRepository($path)
            {
                if (is_dir($path . DIRECTORY_SEPARATOR . '.git')) {
                    return TRUE;
                }
    
                $path = dirname($path);
    
                if ($path == '/') { <-- this should be changed to stop at a defined folder (e.g. the phpunit directory in the projects vendor folder)
                    return FALSE;
                }
    
                return $this->isGitRepository($path);
            }
    
    opened by jenschude 7
  • Incorrect Runner_Version::id() when installed via composer inside project git directory

    Incorrect Runner_Version::id() when installed via composer inside project git directory

    I've PHPUnit 4.3.1 installed using Composer.

    $version = new SebastianBergmann\Version('4.3.1', dirname(dirname(__DIR__)));
    

    The problem is that this returns legacy-version-7-belgium-1829-gd13dcc3 instead of the tagged release. When looking at the code it becomes clear that whenever a .git directory is found (recursively) it will always return a git based hash as version.

    But the .git directory that is found is the one of my own project (the project root). There is no .git directory inside vendor/phpunit/...

    Is there a way to stop this or to limit the level of recursiveness?

    opened by ruudk 6
  • Infinite recursion in latest stable version on windows when not in git repository

    Infinite recursion in latest stable version on windows when not in git repository

    When runing phpcpd it uses as a dependency this lib "Version" ant it produces an infinate recursion and uses up all memory, i Installed using pear, so no git hub repository is present.

    Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\Pr ogram Files (x86)\PHP\pear\SebastianBergmann\Version\Version.php on line 125

    INSTALLED PACKAGES, CHANNEL PEAR.PHPUNIT.DE:
    ============================================
    PACKAGE                   VERSION STATE
    File_Iterator             1.3.3   stable
    FinderFacade              1.0.6   stable
    PHPUnit                   3.7.19  stable
    PHPUnit_MockObject        1.2.3   stable
    PHPUnit_SkeletonGenerator 1.2.0   stable
    PHP_CodeCoverage          1.2.9   stable
    PHP_Timer                 1.0.4   stable
    PHP_TokenStream           1.1.5   stable
    Text_Template             1.1.4   stable
    Version                   1.0.0   stable
    phpcpd                    1.4.1   stable
    phpdcd                    0.9.3   beta
    phploc                    1.7.4   stable
    ppw                       1.0.4   stable
    
    Call Stack:
        0.0002     322912   1. {main}() C:\Program Files (x86)\PHP\phpcpd:0
        0.0165     772056   2. SebastianBergmann\PHPCPD\TextUI\Command->__construct(
    ) C:\Program Files (x86)\PHP\phpcpd:55
        0.0169     797368   3. SebastianBergmann\Version->getVersion() C:\Program Fi
    les (x86)\PHP\pear\SebastianBergmann\PHPCPD\TextUI\Command.php:68
        0.0169     797368   4. SebastianBergmann\Version->getGitInformation() C:\Pro
    gram Files (x86)\PHP\pear\SebastianBergmann\Version\Version.php:81
        0.0169     797368   5. SebastianBergmann\Version->isGitRepository() C:\Progr
    am Files (x86)\PHP\pear\SebastianBergmann\Version\Version.php:103
        0.0170     797472   6. SebastianBergmann\Version->isGitRepository() C:\Progr
    am Files (x86)\PHP\pear\SebastianBergmann\Version\Version.php:135
        0.0170     797576   7. SebastianBergmann\Version->isGitRepository() C:\Progr
    am Files (x86)\PHP\pear\SebastianBergmann\Version\Version.php:135
        0.0171     797672   8. SebastianBergmann\Version->isGitRepository() C:\Progr
    am Files (x86)\PHP\pear\SebastianBergmann\Version\Version.php:135
        0.0171     797744   9. SebastianBergmann\Version->isGitRepository() C:\Progr
    am Files (x86)\PHP\pear\SebastianBergmann\Version\Version.php:135
    
    opened by Gamesh 2
  • Please move code into a subdirectory, SebastianBergmann/Version/autoload.php

    Please move code into a subdirectory, SebastianBergmann/Version/autoload.php

    When trying to checkout PHPUnit as Git submodules in a project, so that I can fork and modify them, I get this error from CodeCoverage:

    • PHP Fatal error: require_once(): Failed opening required 'SebastianBergmann/Version/autoload.php' in /home/installuser/Dropbox/projects/ischool/website/web/lib/php-code-coverage/PHP/CodeCoverage/Autoload.php on line 49

    My include_path is:

    • /home/installuser/Dropbox/projects/ischool/website/tests/../web
    • /home/installuser/Dropbox/projects/ischool/website/tests/../web/lib
    • /home/installuser/Dropbox/projects/ischool/website/tests/../web/lib/phpunit
    • /home/installuser/Dropbox/projects/ischool/website/tests/../web/lib/php-code-coverage
    • .
    • /usr/share/php
    • /usr/share/pear'

    There's no way to resolve this without installing the module globally using PEAR (defeating the point of what I was trying to do, and making the code harder to install) or moving the files around.

    If you could create the directory:

    src/SebastianBergmann/Version
    

    and move src/autoload.php into it, then I could add sebastianbergmann-version/src to my PHP include path, and it would find the files and I would be able to load phpunit and its dependencies from the Git checkout. Thanks :)

    opened by qris 2
  • Switch from exec() to proc_open().

    Switch from exec() to proc_open().

    This enables the use of this component on systems where chdir() or exec() is disabled (but proc_open() is not). This also allows us to forgo the silence operator, which is convenient if xdebug.scream is enabled.

    opened by willemstuursma 0
  • Always use last git segment.

    Always use last git segment.

    This fixes an issue we're currently having where git describe --tags return 1.7.0-beta.2-208-g6d1683e, and thus the version becomes 1.7-208 instead of 1.7-g6d1683e.

    opened by jyggen 0
  • getVersion() fails on Windows hosts

    getVersion() fails on Windows hosts

    When using the getVersion() on a Windows based host it seems that getVersion() gets stuck in an infinite loop while trying to determine if the directory is a Git repo:

     Fatal error: Maximum function nesting level of '100' reached, aborting! in C:\[...]\vendor\sebastian\version\src\Version.php on line 125
    
    Call Stack:
      0.0009     122592   1. {main}() C:\[...]\vendor\sebastian\phpcpd\composer\bin\phpcpd:0
      0.0081     351000   2. SebastianBergmann\PHPCPD\TextUI\Command->__construct() C:\[...]\vendor\sebastian\phpcpd\composer\bin\phpcpd:69
      0.0087     362728   3. SebastianBergmann\Version->getVersion() C:\[...]\vendor\sebastian\phpcpd\src\TextUI\Command.php:68
      0.0087     362864   4. SebastianBergmann\Version->getGitInformation() C:\[...]\vendor\sebastian\version\src\Version.php:81
      0.0087     362904   5. SebastianBergmann\Version->isGitRepository() C:\[...]\vendor\sebastian\version\src\Version.php:103
      0.0092     363080   6. SebastianBergmann\Version->isGitRepository() C:\[...]\vendor\sebastian\version\src\Version.php:135
      0.0095     363176   7. SebastianBergmann\Version->isGitRepository() C:\[...]\vendor\sebastian\version\src\Version.php:135
      0.0099     363272   8. SebastianBergmann\Version->isGitRepository() C:\[...]\vendor\sebastian\version\src\Version.php:135
      0.0102     363360   9. SebastianBergmann\Version->isGitRepository() C:\[...]\vendor\sebastian\version\src\Version.php:135
      0.0105     363440  10. SebastianBergmann\Version->isGitRepository() C:\[...]\vendor\sebastian\version\src\Version.php:135
      [etc]
    

    I checked the code and it seems that the following check fails:

    [line: 131] if ($path == '/') {
    

    because on windows $path will be 'C:'

    opened by zBart 0
  • Explicitly specify git directory when running git

    Explicitly specify git directory when running git

    Backstory

    This was an interesting one track down. The symptom was that PHPStan only worked when run directly, but not when run as part of a git invoked workflow (e.g. through git rebase --exec).

    The cause was that phpstan-drupal includes a file from the Drupal framework that queries the runner to figure out which PHPUnit version is being used.

    The version returned when running git did not match the actual PHPUnit version installed which caused the alias that Drupal created to a PHPUnit bridge class to break.

    Problem

    The Version class uses a git command to find the tag of the currently installed package version for packages that composer has checked out from git. This works when executed directly.

    However, when used in a git based workflow such as git rebase --exec or git bisect then this fails. The cause is that for those commands git will add environment variables that specify the GIT_DIR on which its performing those workflows, so that moving around and calling git commands still operates according to the git repository the workflow is being executed on.

    As a result when Version calls git describe --tags this does not use the .git folder in the working directory of proc_open but instead uses the git folder for which the git workflow is in progress.

    Solution

    To remedy this the Version class should specify --git-dir directly. A few lines above we already know explicitly what git directory we're interested in. Specifying the argument to git overrides any environment variables that may be set. This allows the library to work in automated git workflows invoked from other repositories.

    opened by Kingdutch 2
  • Modernize build automation

    Modernize build automation

    • [ ] Use GitHub Actions instead of Travis CI
    • [ ] Have Composer in tools/composer and managed through composer self-update (see update-tools target in build.xml
    • [ ] Install Psalm using Phive as tools/psalm (phive install --copy psalm)
    • [ ] Install PHP-CS-Fixer using Phive as tools/php-cs-fixer (phive install --copy php-cs-fixer)
    • [ ] Create Psalm configuration
    • [ ] Add Psalm build step to GitHub Actions-based CI workflow
    • [ ] Add PHP-CS-Fixer build step to GitHub Actions-based CI workflow
    opened by sebastianbergmann 0
Owner
Sebastian Bergmann
Sebastian Bergmann is the creator of PHPUnit. He co-founded thePHP.cc and helps PHP teams build better software.
Sebastian Bergmann
A tool for managing SSH key access to any number of servers.

Revons - SSH Key Authority Features Easily manage SSH key access for all accounts on your servers. Manage user access and server-to-server access rule

Revons Community 1 Mar 14, 2022
Private, self-hosted Composer/Satis repository with unlimited private and open-source packages and support for Git, Mercurial, and Subversion.

Private, self-hosted Composer/Satis repository with unlimited private and open-source packages and support for Git, Mercurial, and Subversion. HTTP API, HTTPs support, webhook handler, scheduled builds, Slack and HipChat integration.

Łukasz Lach 112 Nov 24, 2022
CodeFever Community Edition (A Self-hosted Git Services)

CodeFever Community Edition (A Self-hosted Git Services)

PGYER 2.3k Jan 7, 2023
Utility that helps you switch git configurations with ease.

git-profile Utility that helps you switch git configurations with ease Preface It is possible that you have multiple git configurations. For example:

Zeeshan Ahmad 240 Jul 18, 2022
Remote Git Library for PHP

This library provides methods to handle git repositories remotely without having to clone the whole repo. It uses the Symfony process component to run the git client.

Martin Auswöger 4 Dec 14, 2022
Orangescrum is a simple yet powerful free and open source project management software that helps team to organize their tasks, projects and deliver more.

Free, open source Project Management software Introduction Orangescrum is the simple yet powerful free and open source project management software tha

Orangescrum 110 Dec 30, 2022
Get the system resources in PHP, as memory, number of CPU'S, Temperature of CPU or GPU, Operating System, Hard Disk usage, .... Works in Windows & Linux

system-resources. A class to get the hardware resources We can get CPU load, CPU/GPU temperature, free/used memory & Hard disk. Written in PHP It is a

Rafael Martin Soto 10 Oct 15, 2022
PHP Magic Number Detector

PHP Magic Number Detector (PHPMND) phpmnd is a tool that aims to help you to detect magic numbers in your PHP code. By default 0 and 1 are not conside

Povilas Susinskas 514 Dec 14, 2022
The Current US Version of PHP-Nuke Evolution Xtreme v3.0.1b-beta often known as Nuke-Evolution Xtreme. This is a hardened version of PHP-Nuke and is secure and safe. We are currently porting Xtreme over to PHP 8.0.3

2021 Nightly Builds Repository PHP-Nuke Evolution Xtreme Developers TheGhost - Ernest Allen Buffington (Lead Developer) SeaBeast08 - Sebastian Scott B

Ernest Buffington 7 Aug 28, 2022
This is an experiment to export all RFCs from the PHP wiki into Git, including the change history for each RFC (along with the date and author of each change). This is not meant to replace the wiki.

PHP Requests for Comments (RFCs) About This repository is an experiment to export all RFCs from the PHP wiki into Git, including the change history fo

Ben Ramsey 34 Jun 20, 2022
PhpGit - A Git wrapper for PHP 7.1+

PhpGit PhpGit - A Git wrapper for PHP 7.1+ The project is forked from https://github.com/kzykhys/PHPGit Requirements PHP 7.1+ Git Installation Update

PHPPkg 9 Nov 1, 2022
Very flexible git hook manager for php developers

CaptainHook CaptainHook is an easy to use and very flexible git hook library for php developers. It enables you to configure your git hook actions in

CaptainHook 812 Dec 30, 2022
Dobren Dragojević 6 Jun 11, 2023
Adds a compact "easy-sort" mode to Repeater and Repeater Matrix, making those fields easier to sort when there are a large number of items.

Repeater Easy Sort Adds a compact "easy-sort" mode to Repeater and Repeater Matrix, making those fields easier to sort when there are a large number o

Robin Sallis 3 Oct 10, 2021
Laravel package to convert English numbers to Bangla number or Bangla text, Bangla month name and Bangla Money Format

Number to Bangla Number, Word or Month Name in Laravel | Get Wordpress Plugin Laravel package to convert English numbers to Bangla number or Bangla te

Md. Rakibul Islam 50 Dec 26, 2022
Replace, concat strings or change number fields permanently using Grid Options

It's Pimcore Bundle to replace ,concat strings or change number fields permanently using Grid Options. It will save replaced strings directly in object.

LemonMind.com 5 Aug 31, 2022
RealMikrotikBackup - Система резервного копирования для оборудования Mikrotik, с возможностью использования функционала Git для контроля версий конфигураций оборудования.

Real Mikrotik Backup System Real Mikrotik Backup - это система централизованного создания и хранения резервных копий оборудования Mikrotik с функцией

Anton Moroz 23 Dec 18, 2022
Easily manage git hooks in your composer config

composer-git-hooks Manage git hooks easily in your composer configuration. This command line tool makes it easy to implement a consistent project-wide

Ezinwa Okpoechi 985 Jan 3, 2023
Text-mode interface for git

Tig: text-mode interface for Git What is Tig? Tig is an ncurses-based text-mode interface for git. It functions mainly as a Git repository browser, bu

Jonas Fonseca 11.4k Dec 30, 2022