{"id":24793482,"url":"https://github.com/redwerks/node-git-cmd","last_synced_at":"2025-03-24T17:06:26.943Z","repository":{"id":30044336,"uuid":"33593480","full_name":"redwerks/node-git-cmd","owner":"redwerks","description":"A command builder to build functions that will run git commands and extract output from them in a variety of ways.","archived":false,"fork":false,"pushed_at":"2015-09-15T15:01:32.000Z","size":164,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-29T22:02:43.114Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redwerks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-08T08:09:50.000Z","updated_at":"2015-04-11T00:06:54.000Z","dependencies_parsed_at":"2022-08-24T16:52:01.218Z","dependency_job_id":null,"html_url":"https://github.com/redwerks/node-git-cmd","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redwerks%2Fnode-git-cmd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redwerks%2Fnode-git-cmd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redwerks%2Fnode-git-cmd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redwerks%2Fnode-git-cmd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redwerks","download_url":"https://codeload.github.com/redwerks/node-git-cmd/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245315292,"owners_count":20595217,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-29T21:57:17.463Z","updated_at":"2025-03-24T17:06:26.920Z","avatar_url":"https://github.com/redwerks.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![npm version](https://badge.fury.io/js/git-cmd.svg)](http://badge.fury.io/js/git-cmd)\n[![Build Status](https://travis-ci.org/redwerks/node-git-cmd.svg?branch=master)](https://travis-ci.org/redwerks/node-git-cmd)\n[![Dependency Status](https://david-dm.org/redwerks/node-git-cmd.svg)](https://david-dm.org/redwerks/node-git-cmd)\n[![devDependency Status](https://david-dm.org/redwerks/node-git-cmd/dev-status.svg)](https://david-dm.org/redwerks/node-git-cmd#info=devDependencies)\n\ngit-cmd\n=======\ngit-cmd is a node.js command builder meant to be used to build your own functions to interact with git repositories through the `git` command.\n\ngit-cmd executes the git command with a set of arguments for you, gives you an interface to extract this data, and then returns the result as a Bluebird promise.\n\n## Installation\n\n```console\n$ npm install git-cmd\n```\n\n## API documentation\n\n```js\nvar git = require('git-cmd');\n```\n\n### git(args, options) =\u003e *cmd*\nConstruct a git command. The command is not executed at this point.\n\n* **args** (array): The args to pass to the `git` command.\n* **options** (options):\n  * **git** (string) [default=`\"git\"`] the path to the `git` executable.\n  * **cwd** (string) [default=`process.cwd`] the git directory.\n  * **GIT_DIR** (string) the GIT_DIR environment variable.\n\n### **cmd** chained methods\n\n#### cmd.push(arg) =\u003e *cmd*\nThis adds a single argument to the args list. This allows you to construct complex conditional commands.\n\n```js\nfunction clone(url, path, opts) {\n    opts = opts || {};\n    var cmd = git(['clone']);\n    if ( opts.bare ) {\n        cmd.push('--bare');\n    }\n\n    cmd.push(url);\n    cmd.push(path);\n\n    return cmd.pass({prefix: opts.prefix});\n}\n```\n\n#### cmd.pipe(stream) =\u003e *cmd*\nAdd a transform stream to pipe stdout through for the `.capture()`, `.oneline()`, and `.array()` run methods.\n\n```js\nvar LineStream = require('byline').LineStream;\n\nfunction tags() {\n    return git(['tag'], {cwd: cwd})\n        .pipe(new LineStream())\n        .array();\n}\n```\n\n### **cmd** run methods\nThese commands execute the git command and return the result in various ways.\n\n#### cmd.ok() =\u003e *promise*\nExecute the command and resolve with a boolean indicating whether the command succeeded (error code=0, true) or failed (error code \u003e 0, false).\n\nstdout and stderr will be ignored.\n\nThis result is useful if you're doing a boolean test for the presence of something in the git repository and git exits with an error code when it's missing.\n\n##### Example\n\n```js\nfunction hasRef(ref) {\n    return git(['rev-parse', ref], {cwd: cwd}).ok();\n}\n```\n\n#### cmd.pass(options) =\u003e *promise*\nExecute the command and pass all stdout and stderr output through. The promise is resolved simply with true when the process exits.\n\nThis result is useful when running long running commands like `fetch` and `clone` that output progress information to the terminal.\n\n##### Options\n\n* **prefix** (string) [default=`\"\"`] prefix for every line piped to stdout and stderr.\n  * If you are executing multiple git commands in parallel this can identify what output is from what command.\n* **silenceErrors** (boolean) [default=`false`] don't pass through stderr output.\n\n##### Example\n\n```js\nfunction fetchOrigin(ref) {\n    return git(['fetch', 'origin'], {cwd: cwd}).pass();\n}\n\nfunction fetchRepo(name) {\n    return git(['fetch', 'origin'], {cwd: nameToPath(name)}).pass({prefix: name + ': '});\n}\n```\n\n#### cmd.capture(options) =\u003e *promise*\nExecute the command and capture the output into a single buffer or string.\n\nAny stderr output is passed through to to the terminal.\n\nThis is useful for any command you are capturing binary or multi-line data. Especially raw output such as that from cat-file.\n\n##### Options\n\n* **prefix** (string) [default=`\"\"`] prefix for every line piped to stderr.\n* **encoding** (string|`undefined`|`false`) [default=`undefined`] the character encoding of the data to decode.\n* **silenceErrors** (boolean) [default=`false`] don't pass through stderr output.\n\n##### Example\n\n```js\nfunction catBinaryFile(file) {\n    return git(['cat-file', 'blob', util.format('HEAD:%s', file)], {cwd: cwd})\n        .capture();\n}\n\nfunction catTextFile(file) {\n    return git(['cat-file', 'blob', util.format('HEAD:%s', file)], {cwd: cwd})\n        .capture({encoding: 'utf8'});\n}\n```\n\n#### cmd.oneline(options) =\u003e *promise*\nExecute the command and capture the output as a string without a trailing line feed.\n\nAny stderr output is passed through to to the terminal.\n\nThis is useful for any command that simply returns a simple one-line string.\n\n##### Options\n\n* **prefix** (string) [default=`\"\"`] prefix for every line piped to stderr.\n* **silenceErrors** (boolean) [default=`false`] don't pass through stderr output.\n\n##### Example\n\n```js\nfunction getRefHash(ref) {\n    return git(['show-ref', '--hash', ref], {cwd: suite.cwd})\n        .oneline();\n}\n\nfunction getSymbolicRef(ref) {\n    return git(['symbolic-ref', ref], {cwd: cwd})\n        .oneline();\n}\n```\n\n#### cmd.array(options) =\u003e *promise*\nExecute the command and return the result as an array. This method only works when you pipe the output through a transform stream that outputs data in objectMode.\n\nAny stderr output is passed through to to the terminal.\n\nThis is useful when you want to return a list of objects processed through transformation streams.\n\n##### Options\n\n* **prefix** (string) [default=`\"\"`] prefix for every line piped to stderr.\n* **silenceErrors** (boolean) [default=`false`] don't pass through stderr output.\n\n##### Example\n\n```js\nvar LineStream = require('byline').LineStream,\n    es = require('event-stream');\n\nfunction getRefs() {\n    return git(['show-ref'], {cwd: cwd})\n        .pipe(new LineStream())\n        .pipe(es.mapSync(function(line) {\n            var m = line.match(/^([0-9a-f]+) (.+)$/);\n            return {\n                sha1: m[1],\n                ref: m[2]\n            };\n        }))\n        .array();\n}\n\nfunction lsTree(treeIsh) {\n    return git(['ls-tree', treeIsh], {cwd: cwd})\n        .pipe(new LineStream())\n        .pipe(es.mapSync(function(line) {\n            var m = line.match(/^(\\d+) ([^ ]+) ([0-9a-f]+)\\t(.+)$/);\n            return {\n                mode: parseInt(m[1], 10),\n                type: m[2],\n                sha1: m[3],\n                name: m[4]\n            };\n        }))\n        .array();\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredwerks%2Fnode-git-cmd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredwerks%2Fnode-git-cmd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredwerks%2Fnode-git-cmd/lists"}