Skip to content
Snippets Groups Projects
Utils.php 1.57 KiB
Newer Older
  • Learn to ignore specific revisions
  • jurgenhaas's avatar
    jurgenhaas committed
    
    namespace LakeDrops\Component\Composer;
    
    /**
     * Manages composer.json files.
     */
    final class Utils {
    
    
      /**
       * @var string[]
       */
      private static $ignoredGitPatterns;
    
    jurgenhaas's avatar
    jurgenhaas committed
    
    
      /**
       * @var string
       */
      private static $lfsGitPatterns;
    
    
      /**
       * Make sure that git init was called.
       */
    
      protected static function gitInit(): void {
    
        if (!file_exists('.git')) {
          passthru(escapeshellcmd('git init'));
        }
      }
    
    
      public static function git(string $command): void {
    
        passthru(escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command));
      }
    
      /**
       * @param string $pattern
       */
    
      public static function gitIgnore(string $pattern): void {
    
        if (self::$ignoredGitPatterns === NULL) {
          if (file_exists('.gitignore')) {
            self::$ignoredGitPatterns = explode(PHP_EOL, file_get_contents('.gitignore'));
          }
          else {
            self::$ignoredGitPatterns = [];
          }
        }
    
    
    jurgenhaas's avatar
    jurgenhaas committed
        if (in_array($pattern, self::$ignoredGitPatterns, TRUE)) {
    
          return;
        }
    
        self::$ignoredGitPatterns[] = $pattern;
        self::git('ignore ' . $pattern);
      }
    
    
      public static function gitLFS(string $pattern): void {
    
        if (self::$lfsGitPatterns === NULL) {
    
          $output = [];
          exec('git lfs track', $output);
          self::$lfsGitPatterns = implode(PHP_EOL, $output);
        }
    
        if (strpos(self::$lfsGitPatterns, $pattern)) {
          return;
        }
    
        self::$lfsGitPatterns = $pattern . PHP_EOL;
        self::git('lfs track ' . $pattern);
      }
    
    
    jurgenhaas's avatar
    jurgenhaas committed
    }