"Fossies" - the Fresh Open Source Software Archive

Member "cli-1.1280.1/src/lib/project-metadata/target-builders/git.ts" (20 Feb 2024, 1683 Bytes) of package /linux/misc/snyk-cli-1.1280.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 import * as url from 'url';
    2 import subProcess = require('../../sub-process');
    3 import { GitTarget } from '../types';
    4 
    5 // for scp-like syntax [user@]server:project.git
    6 const originRegex = /(.+@)?(.+):(.+$)/;
    7 
    8 export async function getInfo({
    9   isFromContainer,
   10   cwd,
   11 }: {
   12   isFromContainer: boolean;
   13   cwd?: string;
   14 }): Promise<GitTarget | null> {
   15   // safety check
   16   if (isFromContainer) {
   17     return null;
   18   }
   19 
   20   const target: GitTarget = {};
   21 
   22   try {
   23     const origin: string | null | undefined = (
   24       await subProcess.execute('git', ['remote', 'get-url', 'origin'], { cwd })
   25     ).trim();
   26 
   27     if (origin) {
   28       const { protocol, host, pathname = '' } = url.parse(origin);
   29 
   30       // Not handling git:// as it has no connection options
   31       if (host && protocol && ['ssh:', 'http:', 'https:'].includes(protocol)) {
   32         // same format for parseable URLs
   33         target.remoteUrl = `http://${host}${pathname}`;
   34       } else {
   35         const originRes = originRegex.exec(origin);
   36         if (originRes && originRes[2] && originRes[3]) {
   37           target.remoteUrl = `http://${originRes[2]}/${originRes[3]}`;
   38         } else {
   39           // else keep the original
   40           target.remoteUrl = origin;
   41         }
   42       }
   43     }
   44   } catch (err) {
   45     // Swallowing exception since we don't want to break the monitor if there is a problem
   46     // executing git commands.
   47   }
   48 
   49   try {
   50     target.branch = (
   51       await subProcess.execute('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
   52         cwd,
   53       })
   54     ).trim();
   55   } catch (err) {
   56     // Swallowing exception since we don't want to break the monitor if there is a problem
   57     // executing git commands.
   58   }
   59 
   60   return target;
   61 }