GitCli
This project is aimed to create the interfacing to GIT via the Command Line Interface (CLI). This is due to the CLI is always the latest version while library is lacking behind. Furthermore, library for Ruby or Java might not be coming in so soon, being later should be a better in keeping up the changes.
Hence the interfacing with the CLI seems the better way to do that.
This codes are tested using git version 2.25.1, Linux x86_64
Installation
Add this line to your application's Gemfile:
gem 'git_cli'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install git_cli
Usage
Example usage:
require 'git_cli'
# Gvcs is artificial namespace to shield the actual
# GitCli package name. It is designed so that if we
# have another VCS coming all client code can remain
# unchanged since the specific package name is not
# being used in client application. The new VCS provider
# can just provide all functions via the interface
# Only draw back is this cannot run multiple VCS at the
# same time
#
# vcs is encapsulation of general functions (read below)
vcs = Gvcs::Vcs.new
vcs.init(path) # init workspace at the given path
vcs.clone("/source/repos", "/destination/repos") # clone a project
workspace = Gvcs::Workspace.new(vcs, "/any/git/repository")
# workspace now can invoke all supported git operations
Supported GIT Operation
The following operations are supported on the git command line:
- Generall functions (to make a directory become git workspace)
- vcs.init("/path/to/new/workspace")
- vcs.clone("/source/repos","/path/to/workspace")
- Workspace command
- workspace.root_path
git rev-parse --show-toplevel
- workspace.is_workspace?
- Call git status see if error thrown
- workspace.repos
- Returns list of remote repositories
- workspace.clean?
- Return true if there is no new,deleted,modified and staged files
- workspace.add("/path/a","/path/b","/path/c/a")
git add
- workspace.remove_staging("/path/a","/path/b","/path/c/a")
git reset
- workspace.remove_vcs("/path/a","/path/b","/path/c/a")
git rm --cached
- workspace.commit("commit message", { files: ["/path/a"."/path/b"] })
git commit /path/a /path/b -m "commit message"
- workspace.commit_all("commit message")
git commit -am
- workspace.status
- Returns list of GitCli::Delta::VCSItem carries attributes @path, @full and @type
git status
- workspace.modFiles
- Returns modified files (GitCli::Delta::VCSItem) in an array
git diff --name-only --diff-filter=M
- workspace.cftFiles
- Returns conflicted files (GitCli::Delta::VCSItem) in an array
git diff --name-only --diff-filter=U
- workspace.newFiles
- Returns and files (non tracked) (GitCli::Delta::VCSItem) in an array
git ls-files --others --exclude-standard --directory
- workspace.delFiles
- Returns deleted files (GitCli::Delta::VCSItem) in an array
git ls-files -d
- workspace.stgFiles
- Returns staged files (GitCli::Delta::VCSItem) in an array
git diff --name-only --cached
- workspace.reset_file_changes("/path/to/file")
git checkout --
- workspace.reset_all_changes
git reset --hard
- workspace.calculat_distance("origin/HEAD","HEAD")
- Returns integer value how far is it
git rev-list 'origin/HEAD'..'HEAD' --count
- workspace.is_local_ahead_of_remote?("origin/HEAD","branch-main")
- Aggregated from calculate_distance() with default to value fixed at "HEAD"
- Returns boolean
- workspace.is_remote_ahead_of_local?("origin/HEAD","branch-main")
- aggregated from calculate_distance() with default from value fixed at "HEAD"
- Returns boolean
- workspace.push("origin","master")
git push origin master
- workspace.push_with_tags("origin","master")
git push origin master --tags
- workspace.pull("origin","master")
git pull origin master
- workspace.current_branch
- Returns branch name
git branch --show-current
- workspace.local_branches
- Return local branches in an array
git branch
- workspace.remote_branches
- Return remote branches in an array
git branch -r
- workspace.all_branches
- Concate output of local_branches and remote_branches
- Returns array
- workspace.switch_branch("new-branch")
git checkout new-branch
- workspace.create_branch("new-branch")
git branch new-branch
- workspace.download_all_remote_branches_name
git fetch -all
- workspace.merge_branch("development")
git merge development
- workspace.delete_branch("development")
git branch -d development
- workspace.diff
git diff
- workspace.diff_file("/path/a")
git diff /path/a
- workspace.diff_branch("master/HEAD","development/HEAD")
git diff master/HEAD..development/HEAD
- workspace.diff_working_with_last_commit
git diff HEAD^ HEAD
- workspace.diff_index_with_last_commit
git diff --cached
- workspace.ignore("/path/a","/path/b")
- Append entries into .gitignore file
- workspace.ignore_rules
- Read the .gitignore files and returns its content in an array
- workspace.update_ignore_rules("*.log")
- Add non file entries into .gitignore
- workspace.show_log(commit_id)
git show commit_id
- workspace.all_tags
- Returns array
git tag
- workspace.tag_info("tagname", "%H|%ad|%an|%s")
git show tagname --format="%H|%ad|%an|%s"
- workspace.create_tag(tagname)
git tag tagname
- workspace.create_tag(tagname, message)
git tag -a tagname -m message
- workspace.create_tag_from_commit(tagname, commit)
git tag -a tagname commit
- workspace.create_tag_from_commit(tagname, commit, message)
git tag -a tagname -m message commit
- workspace.fetch_tag_to_local
git fetch --all --tags
- workspace.show_tag_detail(tagname)
git show tagname
- workspace.delete_tag(tagname)
git tag -d tagname
- workspace.delete_remote_tag("origin","tagname")
git push origin --delete tagname
- workspace.checkout_tag(tagname, branch)
git checkout tags/tagname -b branch
- workspace.tag_points_at?("HEAD")
- Return boolean
git tag --points-at HEAD
- workspace.remote_config
- Return Hash with repos name as key, points to hash with "push" or "fetch" as key
git remote -vv
- workspace.add_remote(name, url)
git remote add name url
- workspace.remove_remote(name)
git remote remove name
- workspace.stash_changes(msg)
git stash save "msg"
- workspace.stash_all_chanegs(msg)
git stash save --include-untracked
- workspace.stash_all_chanegs(msg, true)
git stash save --include-untracked(-u) --all(-a)
- workspace.stash_list
- Returns boolean and hash of stash info
git stash list
- workspace.stash_restore
git stash apply
- workspace.stash_restore(id)
git stash apply id --> ID can be obtained from stash_list. Something like "stash@{0}"
- workspace.stash_restore_and_remove
git stash pop
- workspace.stash_restore_and_remove(id)
git stash pop id --> ID can be obtained from stash_list. Something like "stash@{0}"
- workspace.stash_to_new_branch(branch)
git stash branch
- workspace.stash_to_new_branch(branch, id)
git stash branch id
- workspace.stash_clear
git stash clear
- workspace.stash_remove
git stash drop
- workspace.stash_remove(id)
git stash drop id
- workspace.root_path
Return Value
Unless otherwise stated, the API shall return an array, with 1st element is the boolean status value running the command line (taken from $?) and 2nd element shall be the standard output of the running command. Note though all command was run with flag '2>&1' which effectively redirected the STDERR to STDOUT which should be captured by the program.
This is due to all running of command line is via the backtick (`) method. The reason to use this is backtick allow interaction with user. If there is an input needed, backtick will actually wait user input at the prompt, although not ideal, but it is the simplest and easiest for now.