Add, Commit & Push to git repository on save - visual-studio-code

In one git repository (workspace), I want every save committed ASAP. What is the easiest way to achieve that with VSCode?
So, when I press CTRL + S, it gets pushed upstream.

This can be done with plugin Run on Save
Here is the example that auto commits and pushes upstream any changes in the directory mydir:
"emeraldwalk.runonsave": {
"commands": [
{
"match": "\\\\mydir\\\\",
"isAsync": false,
"cmd": "cd ${fileDirname}",
},
{
"match": "\\\\mydir\\\\",
"isAsync": false,
"cmd": "git add -A",
},
{
"match": "\\\\mydir\\\\",
"isAsync": false,
"cmd": "git commit -a -m \"vscode autosave\"",
},
{
"match": "\\\\mydir\\\\",
"isAsync": false,
"cmd": "git push",
}
]
}

One way to achieve that is via terminal on VSCode. Change to the topmost directory containing the .git file and run the following commands:
git status
This shall show you the uncommitted changes. Then run the following for the new added changes (except for the remove changes)
git add <fileName>
This shall add all the added contents to index.
Run below for the remove changes.
git rm <filename>
Then, git commit -m <commit msg> to commit the changes.

Related

VS Code cannot find git bash shell

I've reinstalled git on another drive today, and I cannot seem to make vs code figure out where git is.
I've as configuration in settings.json
"terminal.integrated.profiles.windows": {
"Git Bash": {
"path": "D:\\Programs\\Git\\bin",
"source": "Git Bash",
"icon": "terminal-bash"
},
but when vs code starts the error message is
cannot find top level C:\Programs file\git\bin
and
The terminal process "C:\Program Files\Git\bin\bash.exe '--login'" terminated with exit code: 1.
Which is the wrong path.
Yes the path is the correct one in environment variables:
removing source and icon worked:
"Git Bash": {
"path": "D:\\Programs\\Git\\bin",
"source": "Git Bash",
"icon": "terminal-bash"
},
to
"GitBash": {
"path": "D:\\Programs\\Git\\bin"
},

How can I get the user name of committer in each commit in a project in Git?

I am trying to make analysis between issues and commits of a project in git.
I am getting the name and email of the committer and author from git using the command below:
git log --pretty="%an %ae %cn %ce"
I am using 'curl -i "https://api.github.com/repos/<repo-owner>/<repo-name>/issues"' to download the issues. This returns the username[login name] for each issue not the name of the user as it is in the git log report:
{ "url": "https://api.github.com/repos/<repo-owner>/<repo-name>/issues/17625",
"node_id": "MDExOlB1bGxSZXF1ZXN0NTA5NDYyMjgz",
"number": 17625,
"title": "......",
"user": {
**"login": "xxxxxx",**
"id": 43045863,
....
"type": "User",
},
I check if the issue and commit has been made by the same user. Since I cannot get the username of committer, I cannot relate them. Is there a way to get the username of each commit on a project in git?

GitHub Actions: How to access to the log of current build via Terminal

I'm trying to get familiar with Github Actions. I have configured my workflow in a way, that every time I push my code to GitHub, the code will automatically be built and pushed to heroku.
How can I access the build log information in terminal without going to github.com?
With the latest cli/cli tool named gh (1.9.0+), you can simply do
(from your terminal, without going to github.com):
gh run view <jobId> --log
# or
gh run view <jobId> --log-failed
See "Work with GitHub Actions in your terminal with GitHub CLI"
With the new gh run list, you receive an overview of all types of workflow runs whether they were triggered via a push, pull request, webhook, or manual event.
To drill down into the details of a single run, you can use gh run view, optionally going into as much detail as the individual steps of a job.
For more mysterious failures, you can combine a tool like grep with gh run view --log to search across a run’s entire log output.
If --log is too much information, gh run --log-failed will output only the log lines for individual steps that failed.
This is great for getting right to the logs for a failed step instead of having to run grep yourself.
And with GitHub CLI 2.4.0 (Dec. 2021), gh run list comes with a --json flag for JSON export.
Use
curl \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/<github-user>/<repository>/actions/workflows/<workflow.yaml>/runs
https://docs.github.com/en/free-pro-team#latest/rest/reference/actions#list-workflow-runs
This will return a JSON with the following structure:
{
"total_count": 1,
"workflow_runs": [
{
"id": 30433642,
"node_id": "MDEyOldvcmtmbG93IFJ1bjI2OTI4OQ==",
"head_branch": "master",
"head_sha": "acb5820ced9479c074f688cc328bf03f341a511d",
"run_number": 562,
"event": "push",
"status": "queued",
"conclusion": null,
"workflow_id": 159038,
"url": "https://api.github.com/repos/octo-org/octo-repo/actions/runs/30433642",
"html_url": "https://github.com/octo-org/octo-repo/actions/runs/30433642",
"pull_requests": [],
"created_at": "2020-01-22T19:33:08Z",
"updated_at": "2020-01-22T19:33:08Z",
"jobs_url": "https://api.github.com/repos/octo-org/octo-repo/actions/runs/30433642/jobs",
"logs_url": "https://api.github.com/repos/octo-org/octo-repo/actions/runs/30433642/logs",
"check_suite_url": "https://api.github.com/repos/octo-org/octo-repo/check-suites/414944374",
"artifacts_url": "https://api.github.com/repos/octo-org/octo-repo/actions/runs/30433642/artifacts",
"cancel_url": "https://api.github.com/repos/octo-org/octo-repo/actions/runs/30433642/cancel",
"rerun_url": "https://api.github.com/repos/octo-org/octo-repo/actions/runs/30433642/rerun",
"workflow_url": "https://api.github.com/repos/octo-org/octo-repo/actions/workflows/159038",
"head_commit": {...},
"repository": {...},
"head_repository": {...}
]
}
Access the jobs_url with a PAT that has repository admin rights.

Composer requiring github repository fork branch still checks all tags

Short question: How can I make composer require my branch from my fork without checking all tags?
Long question:
I want composer to require a specific branch that I created of a fork I created from twig.
My assumption was, that, when defined correctly, composer directly requires the branch.
Instead first it checks all the tags and after that it loads the branch.
I don't want composer to check the tags, I just want to use the branch.
Is this the correct behaviour or am I requiring the branch incorreclty?
My fork would be github.com/myfork/Twig
My branch would be mybranchname
This is my composer.json
{
"require": {
"twig/twig": "dev-mybranchname"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/myfork/Twig"
}
]
...
...
This is the correct behavior. Composer checks for all tags first before it could get to your branch name.
You can check if your code is fetched from the right repository by checking your composer.lock
Your composer.lock should be like this.
{
"name": "twig/twig",
"version": "dev-mybranchname",
"source": {
"type": "git",
"url": "https://github.com/myfork/Twig",
"reference": <your_revision_number>
},
...
}

VScode will not auto push when I commit changes

I can commit loads of changes, but nothing gets to github.
Its only when I manually click the PUSH function from the menu that it pushes to github.
How can I get it to do this automatically when I commit?
These are my VS GIT settings:
// Whether git is enabled
"git.enabled": true,
// Path to the git executable
"git.path": null,
// Whether auto refreshing is enabled
"git.autorefresh": true,
// Whether auto fetching is enabled
"git.autofetch": true,
// Confirm before synchronizing git repositories
"git.confirmSync": true,
// Controls the git badge counter. `all` counts all changes. `tracked` counts only the tracked changes. `off` turns it off.
"git.countBadge": "all",
// Controls what type of branches are listed when running `Checkout to...`. `all` shows all refs, `local` shows only the local branchs, `tags` shows only tags and `remote` shows only remote branches.
"git.checkoutType": "all",
// Ignores the legacy Git warning
"git.ignoreLegacyWarning": false,
// Ignores the warning when there are too many changes in a repository
"git.ignoreLimitWarning": false,
// The default location where to clone a git repository
"git.defaultCloneDirectory": null,
// Commit all changes when there are not staged changes.
"git.enableSmartCommit": false,
According to this issue on GitHub, this feature does not exist and is not planned to be added.
They suggest using Git hooks to achieve this behavior.
Something like this:
#!/usr/bin/env bash
branch_name=`git symbolic-ref --short HEAD`
retcode=$?
# Only push if branch_name was found (my be empty if in detached head state)
if [ $retcode = 0 ] ; then
#Only push if branch_name is master
if [[ $branch_name = "master" ]] ; then
echo
echo "**** Pushing current branch $branch_name to origin ****"
echo
git push origin $branch_name;
fi
fi
You can check this answer for more details and options.