Same as some of my recent posts: get jekyll serve
working to watch
for changes during a preview of my Jekyll-bootstrap blog.
Since I found the Ruby gems setup too difficult, I switched to Ubuntu on Windows 10 after reading a Ruby setup guide.
The default display settings didn’t look great, so I installed
Ubuntu Mono font. Unluckily, one can’t type Chinese
characters, and the bottom line of tmux keeps jumping up from time
to time. Most importantly, the copy and paste function isn’t
convenient to use: every time I need to paste something, I have to
move the mouse to the top of the window, then right click and select
“Modify” → “Paste”. That’s too slow when compared to <S-Ins>
in
mintty which is shipped with Git for Windows.
However, on WLS, aptitude takes good care of package dependencies. That’s much better for installing necessary stuff for blogging with frameworks like Jekyll.
How can I run WLS using a mintty shell?
Use wsl-terminal.
Firstly, I’ve revised the use of chown
, chgrp
and chmod
, and
learnt the function of umask
.
u
, g
, o
stand for “user”, “group” and “others” respectively.r
, w
, x
can be +
or -
.chmod og-w [target-dir]
.Unfortunately, the Windows 10 bash creates folders and files with
permissions 777 and 666 respectively. The comments for
Microsoft/BashOnWindows#352 solved this problem: instead of
putting umask 022
in /etc/profile
or ~/.profile
, it should be
added in ~/.bashrc
because the login mechanism on WLS is different
from a usual GNU/Linux OS. I think the profiles aren’t process
during WLS startup after having added a simple statement for echoing a
few words in ~/.profile
. Lilred’s method is even better.
sh Added this to your '~/.bashrc' https://git.io/vM0xz source
# Note: Bash on Windows does not currently apply umask properly.
if [ "$(umask)" = "0000" ]; then
umask 022
fi
Secondly, I’ve learnt the meaning of eval
.
user@OWNER-PC:~$ ssh-agent
SSH_AUTH_SOCK=/tmp/ssh-xiwtTnVlI90S/agent.6636; export SSH_AUTH_SOCK;
SSH_AGENT_PID=5764; export SSH_AGENT_PID;
echo Agent pid 5764;
user@OWNER-PC:~$ ssh-add
Could not open a connection to your authentication agent.
From user456814’s answer on a Stack Overflow question
about SSH authentication agent, we can see that ssh-agent
returns
the commands to be copied, pasted and ran in the terminal. The
command eval
saves these steps by taking the output as the command
input.
In the previous post in this series, I included three Vim editor commands.
As I’ve already typed hundreds of lines of source code, I feel tired
and I often make mistakes while typing those commands despite these
two command mode mappings in my ~/.vimrc
.
1
2
3
“ Practical Vim Tip 34: Avoid cursors when rcl’g cmd from hist
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
Convert these three commands into a shell script. Since I’m using
FuzzyFinder, I assume that the current working directory
(as shown by :pwd
) is the root of the local repository for the
[Octopress] blog.
1
2
3
#!/bin/sh
sed '1,8d' $1 | kramdown > temp.html~
cat {temp,script}.html~ > tempmj.html~
~
after the file extension names .html
since I had
added *~
in my .gitignore
and I didn’t want these files
(including this shell script) to be tracked by Git.mklink
in cmd
.
(ran as admin) Other options are more complicated:
- Graphical programs: more disk space is consumed for programs
rarely used which have command line alternatives.
- Windows Power Shell: this method won’t work for Home
edition. (That’s my case.) I’m quite satisfied by the
current edition and I don’t see any reason to spend
a thousand dollar and a whole day to have it changed to Pro
version for functionalities that are too advanced for me.While I was writing this post, I used
grep -e "\[kramdown\]" %:h/*.markdown
in the current buffer in order to quickly retrieve the links to
websites to which I had previously referred. To escape the square
brackets and the search pattern, the flag -e
is added in between the
utility name and the pattern string. I tried using marks, but I had
forgotten the difference between a mark named with a small letter and
a capital letter.
Mark | Range |
---|---|
small | local |
capital | global |
To know the page layout of a website, notably my blogs, on mobile devices before publishing it.
I used ifconfig
to check the IP address of my desktop where the
preview site was hosted. It’s 192.168.1.5
. When I typed in this
address followed by a colon and the port number 4000
, then the
browser said “connection timeout” after loading for a while.
I spent a day to identify the cause of the problem.
Actually, the website should be hosted at the internal IP address
shown by ifconfig
instead of localhost
. Since I switch off the
router before going to bed every day, the internal IP address for my
desktop changes daily. Therefore, one uses 0.0.0.0
to host the site
on all IP addresses that the machine possesses.
In Jekyll-Bootstrap, one may simply add host : 0.0.0.0
in
_config.yml
; in Octopress, one may find the line containing
server_port
in the middle of Rakefile
and add --host
#{listen_host}
.1
``` ruby Changes in Rakefile http://www.jmlog.com/octopress-preview-config-in-vagrant/ server_port = “4000” # port for preview server eg. localhost:4000 listen_host = “0.0.0.0” # bind to all available IPs
rackupPid = Process.spawn(“rackup –port #{server_port} –host #{listen_host}”) ```
On Ubuntu, I used UFW to open the port 4000
to all devices at home.
(The netmask of my home router has 24 bits, which is the usual
setting.)
sudo ufw allow from 192.168.1.0/24 to any port 4000:4001 proto tcp
Rakefile
which allows the specification of the host address./24
in 192.168.1.0/24
means after reading
this chapter of the guide.I hadn’t used Vim on my GNU/Linux desktop for a month. When I used it again, an error message was shown on the screen.
[owner@localhost ~]$ vi
Error detected while processing
/home/owner/.vim/bundle/nerdtree/plugin/NERD_tree.vim:
line 153:
E117: Unknown function: nerdtree#ui_glue#setupCommands
Error detected while processing function nerdtree#postSourceActions:
line 2:
E117: Unknown function: nerdtree#ui_glue#createDefaultBindings
Press ENTER or type command to continue
Since I was occupied with another blog, thinking that the problem would be quickly fixed by an update of the NERD tree, a Vim plugin for showing a file tree in a sidebar, I didn’t want to find out the cause.
During this Vundle update, an exclamation mark with red background
was displayed on the LHS of the item Plugin 'scrooloose/nerdtree'
in
the list contained in the status bar. I then checked Vundle’s update
log.
[2016-08-12 15:18:40] Plugin scrooloose/nerdtree
[2016-08-12 15:18:40] $ cd '/home/owner/.vim/bundle/nerdtree' && git pull && git submodule update --init --recursive
[2016-08-12 15:18:40] > error: object file .git/objects/2e/2b649232d6ae4d02d74793e5da0ee08480ad8d is empty
[2016-08-12 15:18:40] > error: object file .git/objects/2e/2b649232d6ae4d02d74793e5da0ee08480ad8d is empty
[2016-08-12 15:18:40] > fatal: loose object 2e2b649232d6ae4d02d74793e5da0ee08480ad8d (stored in .git/objects/2e/2b649232d6ae4d02d74793e5da0ee08480ad8d) is corrupt
[2016-08-12 15:18:40] > fatal: The remote end hung up unexpectedly
[2016-08-12 15:18:40] >
While I was trying to update this plugin manually by typing in Git commands in bash, the electricity supply of my desktop went off suddenly. After it had been switched on again, I tried typing in the commands again, and I got the same error.
How can the Git repository be restored to its previous state, so that it’s intact?
This problem can be fixed by simply undoing all changes done to the
directory .git/
. First, a list of recently changed files is needed,
and find
can be used for this purpose. I searched “ls filter by
time”, then this question about ls
on Unix & Linux
Stack Exchange appeared in the search results. After reading the
first answer, I tried to find the defective binary objects in
.git/objects
.
[owner@localhost ~/.vim/bundle/nerdtree/.git]$ find . -maxdepth 1 -mindepth 1 \
> -mtime -1 -type d -print -and -exec ls -lt {} \;
./2e
total 20
-r--r--r-- 1 owner owner 0 Aug 12 15:18 2b649232d6ae4d02d74793e5da0ee08480ad8d
-r--r--r-- 1 owner owner 256 May 12 20:38 c9b3dd9d6b8d11f2a3c12924f88588a846517d
./7e
total 44
-r--r--r-- 1 owner owner 341 May 12 20:38 8be22b1d5052e9140fc7c7b840755980cb960
9
-r--r--r-- 1 owner owner 49 May 12 20:38 9492c9f3b3b463114c0b6308a8ccd46ce3869
2
-r--r--r-- 1 owner owner 3029 Nov 1 2014 12c042bb8f5ab387a2bd0aabcfe30fce25027
f
./a0
total 20
-r--r--r-- 1 owner owner 0 Aug 12 15:43 019ff4d6b033f37efd9b1a99daf6c9797796b1
-r--r--r-- 1 owner owner 468 May 12 20:38 b89b83d8cd02f8043840a48b0b2cf3724bee1c
./29
total 20
-r--r--r-- 1 owner owner 0 Aug 12 15:43 24ab207162ca18cf5844d00b351ade1b5122f6
-r--r--r-- 1 owner owner 342 May 12 20:38 0935cab8f1e7dec23bb3bda0e2f7dd84c867d9
./38
total 20
-r--r--r-- 1 owner owner 0 Aug 12 15:43 d79fd1123213f2ceb3d6c32e8dad42bd587ec8
-r--r--r-- 1 owner owner 55 May 15 2015 569aab72ab0cbf466c40658ab9a3d86b55263e
./d7
total 8
-r--r--r-- 1 owner owner 0 Aug 12 15:43 9f81cd6e309fa01e063f3e12fb7615e712d7f4
./2a
total 36
-r--r--r-- 1 owner owner 0 Aug 12 15:43 a3bece220f147f85172fe43430d069276d835
c
-r--r--r-- 1 owner owner 7025 Jun 24 2015 f07ddbaf243cef8de791b45e7ff4232025a93
6
-r--r--r-- 1 owner owner 277 May 15 2015 a1964c1ba8fd40f3fdebf64efd0ba3756115a
f
./b7
total 20
-r--r--r-- 1 owner owner 0 Aug 12 15:43 23fee57c918ba3fd06057398e0a905fde7b80e
-r--r--r-- 1 owner owner 277 May 15 2015 2300417059ab5c52ac26c318e6b67bcefbe1e2
./c9
total 20
-r--r--r-- 1 owner owner 0 Aug 12 15:43 9312442b0961241eb0de3d6d65893c52f4d9d1
-r--r--r-- 1 owner owner 49 May 15 2015 ab6021b2473c60ba443c5ad197f2e3e3692895
./e8
total 20
-r--r--r-- 1 owner owner 0 Aug 12 15:43 7e67fafb0d6c63b790b37ab46f67cf0654ea22
-r--r--r-- 1 owner owner 466 May 15 2015 258b21d3a02dfcc0c5c10f3ab186763ae0c38b
./4c
total 36
-r--r--r-- 1 owner owner 0 Aug 12 15:43 bfb3869eb16ca691d93e1a3a05d375e87c1c7
7
-r--r--r-- 1 owner owner 57 May 15 2015 be8a2baee075d0935e3af349d85c2553d77bf
8
-r--r--r-- 1 owner owner 6830 Nov 1 2014 df1a366dbdf08c5f7412ff867d859d6818376
4
Objects are grouped according to the first two digits of their Git hash. The following list is easier to read.
[owner@localhost ~/.vim/bundle/nerdtree/.git/objects]$ find . -mindepth 2 \
> -maxdepth 2 -mtime 0 -type f -exec file {} \;
./2e/2b649232d6ae4d02d74793e5da0ee08480ad8d: empty
./7e/e42eb25e86a6825cdba370f20d2e146ed61ae6: empty
./a0/019ff4d6b033f37efd9b1a99daf6c9797796b1: empty
./29/24ab207162ca18cf5844d00b351ade1b5122f6: empty
./38/d79fd1123213f2ceb3d6c32e8dad42bd587ec8: empty
./d7/9f81cd6e309fa01e063f3e12fb7615e712d7f4: empty
./2a/a3bece220f147f85172fe43430d069276d835c: empty
./b7/23fee57c918ba3fd06057398e0a905fde7b80e: empty
./c9/9312442b0961241eb0de3d6d65893c52f4d9d1: empty
./e8/7e67fafb0d6c63b790b37ab46f67cf0654ea22: empty
./4c/bfb3869eb16ca691d93e1a3a05d375e87c1c77: empty
I removed these files and typed git status
.
[owner@localhost ~/.vim/bundle/nerdtree]$ git status
fatal: bad object HEAD
I googled “invalid sha1 pointer in cache-tree”, and read a webpage a
Stack Overflow post on empty Git objects. The command
git fsck --full
can be used to check the integrity of a Git
repository.
[owner@localhost ~/.vim/bundle/nerdtree/.git]$ git fsck --full
Checking object directories: 100% (256/256), done.
Checking objects: 100% (3525/3525), done.
error: HEAD: invalid sha1 pointer 2e2b649232d6ae4d02d74793e5da0ee08480ad8d
error: refs/heads/master: invalid sha1 pointer 2e2b649232d6ae4d02d74793e5da0ee08
480ad8d
error: refs/remotes/origin/HEAD: invalid sha1 pointer 2e2b649232d6ae4d02d74793e5
da0ee08480ad8d
error: refs/remotes/origin/master: invalid sha1 pointer 2e2b649232d6ae4d02d74793
e5da0ee08480ad8d
error: HEAD: invalid reflog entry 2e2b649232d6ae4d02d74793e5da0ee08480ad8d
error: refs/heads/master: invalid reflog entry 2e2b649232d6ae4d02d74793e5da0ee08
480ad8d
error: refs/remotes/origin/master: invalid reflog entry 2e2b649232d6ae4d02d74793
e5da0ee08480ad8d
error: d79f81cd6e309fa01e063f3e12fb7615e712d7f4: invalid sha1 pointer in cache-t
ree
missing blob 2aa3bece220f147f85172fe43430d069276d835c
The above message inspired me to make the following list of files changed due to this failed Git pull.
[owner@localhost ~/.vim/bundle/nerdtree/.git]$ find . -mtime 0 -type f -print
./refs/heads/master
./refs/remotes/origin/master
./FETCH_HEAD
./logs/HEAD
./logs/refs/heads/master
./logs/refs/remotes/origin/master
./ORIG_HEAD
./index
I looked at the relevant files in the folder logs/refs/
. The change
time in Unix timestamp, the old and new commit IDs were found.
I restored these files (including the logs) with the help of these
logs except FETCH_HEAD
and index
.
At this stage, git status
would still throw out an error if it was
invoked. However, since the references to HEAD
and ORIG_HEAD
had
been manually set, git reset --hard HEAD
could restore the Git
repository.
Finally, I could update the NERD Tree.
[owner@localhost ~/.vim/bundle/nerdtree]$ git pull
remote: Counting objects: 11, done.
remote: Total 11 (delta 3), reused 3 (delta 3), pack-reused 8
Unpacking objects: 100% (11/11), done.
From https://github.com/scrooloose/nerdtree
d280b15..2e2b649 master -> origin/master
Updating d280b15..2e2b649
Fast-forward
autoload/nerdtree/ui_glue.vim | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
-maxdepth
and -mindepth
.-type
)
-mtime [n]
: last modified time equal to n
*24
hours. (rounded down to an integer)
-mtime +[n]
: modified before n
*24
hours.-mtime -[n]
: modified within the last n
*24
hours.-mmin [n]
: modified time calculated in number of minutes
instead of days.-atime [n]
, -amin [n]
: last access time-ctime [n]
, -cmin [n]
: last change of file status-anewer
, -cnewer
, -newer
: followed by filename (-mnewer
doesn’t exist.)-daystart
: use the start of day for calculations. (Preceed
-mtime
)Use -t
flag to sort files using their last modified time in
descending order.
One can find files from a Git SHA1 hash using git ls-tree -r <hash>
.
To search contents in commits, you may refer to a
question about Git on Stack Overflow.
When I wrote my list of low-level commands last year, I
couldn’t understand the role of index
. Now, I know that the
binary objects stored in objets/
and the references can work
independently of index
, which can be re-created by git
write-tree
.
<C-r>
. If the
search fails, one may cancel it by <C-g>
. However, how can one
search forward through the command history instead of backword?
The answer is simple: add stty -ixon
to your BASHRC.