.gitignore
One of the main tasks to do after ʻInitial commit of a project is to "create a nice
.gitignore` from gitignore.io, right? [^ 1] [^ 2]
** Is there? ** **
There is no problem if it can be operated as it is at the beginning,
That may come out.
** It will come out. ** **
In case (1), you may have to go to gitignore.io again and regenerate it.
In the case of (2), I have to write it somewhere in the template-generated .gitignore
.
If this happens, you may feel some trouble each time you respond to (1). Perhaps
So I wondered if I could make something like "an editor that simply writes" language name "and" language ignore "in a nice way when editing .gitignore
. " Isn't the UI like lazygit cool?
If you can do it, it would be convenient to throw in all the patterns you want to ignore in the derivation.
However, I don't currently have the ability to create editors in this format.
I tried a super-simple implementation that looks like MVP. Here => vigi = vi gitignore
When you run the vigi
command,
# ~~~~~~
Open a file like this with vim
and
.idea
# ~~~~~~
python
virtualenv
If you edit like this and exit vim
,
.idea
# ~~~~~~
# https://www.gitignore.io/api/python,Contents of virtualenv
It will create a .gitignore
in a state like this.
By the way, if you execute the vigi
command in this state, the gitignore.io part will be information only in the language. Easy to edit ... I think.
A style that creates a temporary file with the name .gi
, edits it, and converts it to .gitignore
when it is saved.
import subprocess
from pathlib import Path
from subprocess import CalledProcessError, PIPE
def main():
gi_path = Path.cwd() / '.gi'
gitignore_path = Path.cwd() / '.gitignore'
try:
# Convert from ``.gitignore`` to ``.gi``
if gitignore_path.exists():
encode_gitignore(gitignore_path, gi_path)
else:
with gi_path.open('w') as fp:
fp.write(dedent(gi_template).strip())
# Spawn vim
vim_args = ['/usr/bin/vim', str(gi_path)]
vim_proc = subprocess.run(vim_args, check=True)
# Convert from ``.gi`` to ``.gitignore``
decode_gitignore(gi_path, gitignore_path)
except CalledProcessError:
pass
finally:
# Remove ``.gi``
gi_path.unlink()
A rough style that calls vim
with subprocess.run
and is OK if there are no CalledProcessError
exceptions. It's easy and good.
.gi
I'm using try
~ finally
and finally throwing away the .gi
file. If there is an error, .gitignore
is not edited, so it is environmentally friendly.
.gitignore
based on the materialThe .gi
file is normally on the filesystem at this point, so it feels like opening it as a normal file.
Then, put together the information on the language side and pass it to gitignore.io.
Only when the HTTP status is 404, I save the response as usual, but
This is because all cases that "contain information that does not exist in the master" seem to be 404.
It's the only thing that can be said to be a little ingenuity.
def decode_gitignore(src, dest):
custom = []
online = []
with src.open() as fp:
# .Open the gi file and separate the front and back of the delimiter, etc.
if online:
url = f'https://www.gitignore.io/api/{",".join(online)}'
req = Request(url, None, headers={'User-Agent': 'vigi'})
try:
resp = urlopen(req)
custom.append(resp.read().decode())
except HTTPError as err:
if err.code == 404:
custom.append(resp.read().decode())
raise err
with dest.open('w') as fp:
fp.write('\n'.join(custom))
For the time being, .gitignore
in the vigi
project was created based on this command.
I think it is a merit that the number of lines displayed at the time of editing is significantly reduced. In that sense, he did his job as an MVP properly.
However, I don't know what kind of pattern is included in the language master until I finish the conversion to .gitignore
.
If I wanted to develop it a little more, I had to pull out the language master from the pattern candidates properly.
I don't know if it's really good. ~~ Maybe it's suitable for training in some language? ~~
[^ 1]: There is a project template, but please forget it.
[^ 2]: You can also manage it with dotfiles
, but forget about this too