git-utils
A few useful command to get git infos.
Required node 10 or higher.
Prerequisites
The library is a wrapper around the git
binary you have to install git before use this library.
Commands
Clone
Clone a remote repository
const { clone } = require('@ert78gb/git-utils')
await clone({
url: 'https://github.com/ert78gb/git-utils-test.git',
cwd: '/Users/User/git/git-utils-test',
})
Options
parameter (type) | default value | description |
---|
url (string) | empty | HTTPS url of the remote repository |
cwd (string) | inherited | Where to clone the remote repository. Inherited from the Node.js context |
Checkout Branch
Checkout existing branch.
const { checkoutBranch } = require('@ert78gb/git-utils')
await checkoutBranch({
branchName: 'my-branch',
cwd: 'path of the git repo',
})
Options
parameter (type) | default value | description |
---|
branchName (string) | empty | Name of the branch that would like to checkout |
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
Get Current Branch
Current branch name.
const { getCurrentBranchName } = require('@ert78gb/git-utils')
const branch = await checkoutBranch({
cwd: 'path of the git repo',
})
console.log(branch)
Options
parameter (type) | default value | description |
---|
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
Get Current Sha
The current SHA1 hash.
const { getCurrentSha } = require('@ert78gb/git-utils')
const sha = await getCurrentSha({
cwd: 'path of the git repo',
})
console.log(sha)
Options
parameter (type) | default value | description |
---|
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
Get File Changes
All commit info of a file path.
The array is ordered by commit date in descending order.
const { getFileChanges } = require('@ert78gb/git-utils')
const commitInfos = await getFileChanges({
cwd: 'path of the git repo',
path: 'folder/file.path'
})
console.log(commitInfos)
Options
parameter (type) | default value | description |
---|
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
path (string) | | The path of the file from the repository root |
Get Commit Info
The latest commit info.
const { getCommitInfo } = require('@ert78gb/git-utils')
const info = await getCommitInfo({
cwd: 'path of the git repo',
})
console.log(info)
Options
parameter (type) | default value | description |
---|
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
Output
Array<{
sha:string,
author: string,
date: Date,
message: string
}>
Get Parent SHA
Query the parent shas of the latest commit or the provided SHA.
If a commit is a merge commit it has multiple parent
const { getParentShas } = require('@ert78gb/git-utils')
const shas = await getCommitInfo({
sha: '7f15cc49347592fbd60ea839f7e06be553965234',
cwd: 'path of the git repo',
})
console.log(shas)
Options
parameter (type) | default value | description |
---|
sha (string) | empty | SHA of the commit which parents you would like to get. You can use branch name too. |
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
Get Merge base
Get the merge base of 2 sha or 2 branch
const { getMergeBase } = require('@ert78gb/git-utils')
const sha = await getMergeBase({
sha1: '7f15cc49347592fbd60ea839f7e06be553965234',
sha2: 'cde7266d8004b1391ce033fd86f0ffe4a1ce1793',
cwd: 'path of the git repo',
})
console.log(sha)
Options
parameter (type) | default value | description |
---|
sha1 (string) | empty | SHA or branch name which part of the merge |
sha2 (string) | sha of the latest commit | SHA or branch name which part of the merge. The default value is the latest commit in the current branch |
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
Get Changed Files
Get the changed files of 2 sha or 2 commit
const { getChangedFiles } = require('@ert78gb/git-utils')
const changedFiles = await getChangedFiles({
sha1: '7f15cc49347592fbd60ea839f7e06be553965234',
sha2: 'cde7266d8004b1391ce033fd86f0ffe4a1ce1793',
cwd: 'path of the git repo',
})
console.log(changedFiles)
Options
parameter (type) | default value | description |
---|
sha1 (string) | empty | SHA or branch name which part of the diff |
sha2 (string) | sha of the latest commit | SHA or branch name which diff. The default value is the latest commit in the current branch |
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
Output
Array<{
action: string,
path: string
}>
Values of the actions
Get Remote Tags
const { getRemoteTags } = require('@ert78gb/git-utils')
const tags = await getRemoteTags({
remote: 'https://github.com/ert78gb/git-utils-test.git',
prefix: 'org/feature/*',
})
console.log(tags)
Options
parameter (type) | default value | description |
---|
remote (string) | 'origin' | URL of the remote git repo or alias remote of the local repo |
prefix (string) | empty | return only tags that match this pattern. If the * missing from the end of the string it will exact match not just prefix |
cwd (string) | inherited | Path of the git repository. Inherited from the Node.js context |
Output
Map<string, string>