Open text editor in a ruby script - ruby

(Disclaimer: I posted a similar question earlier: In a ruby script, how to ask git to open its message editor. I decided to post this as a separate question because I think this question is more generic and thus possibly more applicable to other programmers. However, I kept the git-related question because I'm not sure if an answer here may apply there too. If there are rules against this, let me know)
I'm currently working on a command-line ruby gem that automates the "rebase + no-ff merging" workflow discussed at https://gist.github.com/jbenet/ee6c9ac48068889b0912. You can find the WIP code for this gem at https://github.com/gsmendoza/git_pretty_accept/tree/git_pretty_accept. The gem would do something like this:
`vi some_temp_file.txt`
`git co master`
`git pull`
`git co pull_request`
`git rebase master`
`git co master`
merge_message = File.read('some_temp_file.txt')
`git merge --message "#{merge_message}" --no-ff pull_request`
`git push`
`git branch -d pull_request`
`git push origin:pull_request`
When I try to run these git commands via ruby, vi some_temp_file.txt doesn't open the text editor like I hope it would. Instead, I see a warning "Vim: Warning: Output is not to a terminal" and the script just kind of hangs.
Any ideas?

To run a bash command from within a ruby script, you can use the system() command:
http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-system
This should produce the desired behavior in your particular case:
#!/usr/bin/ruby
system('vim', 'some_temp_file.txt')
Running this script from the command line opens the given file in Vim.

system("#{ENV['EDITOR']} 'yourfile.txt")
Will use the editor defined by the user in the EDITOR environment variable.

Related

in VSCODE, copy and paste into a git bash shell has bad / incorrect characters

If I have a document open in VSCode in windows, and I copy some text from a document like:
git submodule update --init --recursive
and I paste it into a git bash terminal in VSCODE, it has incorrect characters:
git submodule update --init --recursiveFunction Key 9
Is this something configured incorrectly?
I had this problem with git bash.
I learned to paste things to the terminal using shift + insert.
Since this, I had never problems with unwanted characters. I hope that solves your problem.

Git list commands sometimes emulate a multi-page terminal, but I'd like output to always be dumped to stdout [duplicate]

This question already has answers here:
Git branch command behaves like 'less'
(9 answers)
Closed 11 months ago.
I'm a little confused with the following behavior of git commands in the Windows Command prompt.
This shows a result comparable to VIM (I think) with interactive list
git branch -a
Result is something like the following and I've no idea what key-combinations to use to scroll through the output (I did notice that q ends the output).
But this shows a "normal" result by just dumping the output to stdout
git show-ref --tags --heads
which is what I prefer:
How to make all output be just dumped to stdout?
I'd prefer if all commands work the same way. Esp if I want to automate a git command in a batch script or something, I cannot use some kind of interactive shell.
I noticed that this behavior seems to be caused by the TERM=msys environment variable setting, which, if absent, behaves the same. I don't know if there's a different setting I can use.
In the end, while the colored result may be fun, I can get that by using Git BASH. But when I run the commands directly from the Windows Command, I'd expect them all to just echo the output to stdout.
I can't remember that this was the behavior previously, so my guess is that I installed something that f'ed things up ;). I'm on Windows 10 for this machine, git, bash etc are latest.
This is what git calls a pager. You can disable it for a particular command with:
git --no-pager branch -a
(Note that --no-pager goes between git and the subcommand, because it is a global switch).
If you want to configure your system so you don't have to remember that, from man git-config:
pager.<cmd>
If the value is boolean, turns on or off pagination of the output of a particular Git subcommand when writing to a tty. Otherwise, turns on pagination for the subcommand using the pager specified by the value of pager.<cmd>. If --paginate or --no-pager is specified on the command line, it takes precedence over this option. To disable pagination for all commands, set core.pager or GIT_PAGER to cat.
That is, you can disable the pager for git-branch only with git config --global pager.branch false, for example.
And if you want to disable it for all commands do git config --global core.pager cat. Or you can try to set the environment variable GIT_PAGER=cat, of course.

How does one disable Git's HTML help (on Windows)?

I installed Github for Windows which comes with the Git command line client, and whenever I forget a switch or something and want to use --help, instead of dumping to the console it's launching a browser. Is there a way to ask Git to dump the help to the console (as it does in most Unixen by default) instead of launching a browser?
In windows
git <command> -h
will write help to the terminal output
git <command> --help
will pop up a browser window
This is a frail workaround, but if you just want a quick usage summary, feed the git sub-command of your choice a deliberately bad option name. I tried "--halp". For example:
$ git stash --halp
error: unknown option for 'stash save': --halp
To provide a message, use git stash save -- '--halp'
usage: git core\git-stash list [<options>]
or: git core\git-stash show [<stash>]
or: git core\git-stash drop [-q|--quiet] [<stash>]
or: git core\git-stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
or: git core\git-stash branch <branchname> [<stash>]
or: git core\git-stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]]
or: git core\git-stash clear
I can't say for sure that "halp" will always be rejected, but it seems to get the job done. Hopefully it'll never get interpreted as a usable parameter. This is probably better than random typing, for example, since you might randomly type in correct input.
For Linux systems you could set this with git config --global help.format <web|man|info>. Unfortunately the man pages are not part of the Git for Windows bundle so only 'web' works.

Git pre-commit hook

I'm new to git hooks. I'm not able to understand below pre-commit hook. Can anyone tell me how this works please.Here my doubt is how grep will be happened in committed files as we are not taking those files anywhere. Sorry if am asking wrong question but please help me in understanding git hooks..
#!/usr/bin/env ruby
if `grep -rls "require 'ruby-debug'; raise" *` != ""
puts "You twit, you've left a debugger in!"
exit(1)
end
You should rather grep on indexed (cached) files, instead of your working tree.
Otherwise, your grep could find debug instructions in files (or part of files) which aren't part of the next commit.
See "Git pre-commit hook : changed/added files":
git diff --cached --name-only --diff-filter=ACM
As explained in "Why You Need a Git Pre-Commit Hook and Why Most Are Wrong":
Most test against whatever files are currently on disk, not what is in the staging area (the files actually being committed).
The approach if that hook is a bit different: it stashes every work in progress before searching the files.
def main(all_files):
# Stash any changes to the working tree that are not going to be committed
subprocess.call(['git', 'stash', '-u', '--keep-index'], stdout=subprocess.PIPE)

Use Xcode 4 as Git difftool

I want to use Xcode 4's "Version Editor" view as my standard difftool for Git.
In other words, I want to be able to set this option:
git config --global diff.external <XCODE>
And have the diff open in Xcode's diff viewer (because it's cool).
Is this possible? How?
Sadly not possible. Here's hoping Apple changes that someday though.
I'm guessing you already know the following, but for the benefit of others who may not, you can use Apple's FileMerge application instead for a similar, albeit somewhat lesser, experience with a command like:
git difftool path/to/file
My git defaults to using FileMerge as the difftool, but you can configure it explicitly with:
git config --global diff.tool opendiff
(This stops git from listing the candidate tools every time too.) I also like to disable git's difftool pre-launch prompting:
git config --global difftool.prompt false
It is possible to configure git so that git diff will invoke FileMerge as well, or instead. (I prefer to just leave git diff the way it is myself.) If you want that you first need to create a shell script to map the appropriate arguments to opendiff:
#!/bin/sh
/usr/bin/opendiff "$2" "$5" -merge "$1"
and then run
git config --global diff.external /path/to/shell/script

Resources