-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathghistory
executable file
·78 lines (69 loc) · 3.16 KB
/
ghistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env sh
# A way of viewing editted files in a simple interface You can list all
# files or just files within the current directory.
#
# Usage:
# Show the most recently editted files in the current directory
# `ghistory log --pretty='' --name-only | awk '!seen[$0]++' | tac`
#
# ... or use this more convenient alias
# `ghistory CHANGED`
#
# See all changes from the previous revision, in the current directory
# `ghistory diff HEAD^ -- .`
#
# Find out what changed in the last week at the current directory
# `ghistory diff \"HEAD@{1 week ago}\" -- .`
#
# See the changed files for the last 10 days
# `ghistory log --since=\"10 days ago\" --pretty=format: --name-only | sort | uniq`
#
# See the changed files for the last 10 days in the current directory
# `ghistory log --since=\"10 days ago\" --pretty=format: --name-only -- . | sort | uniq`
#
# Get the all-time diff of the files in the current directory
# Reference: https://stackoverflow.com/a/5189296/3626104
# `ghistory diff `ghistory rev-list --max-parents=0 HEAD` -- .`
# or, using a hard-coded alias:
# `ghistory diff ORIGINAL -- .`
#
# Search any content in the files in the current directory
# `ghistory grep -n foo -- .`
#
# Search for git commits which contain a phrase through EVERY revision of every file in the current directory
# `ghistory log -Sthing -- . | grep commit`
#
# Get the search contents for each commit that contains a phrase through EVERY revision of every file in the current directory
# `ghistory log -Sthing -- . | grep commit | cut -d" " -f2 | xargs -I{} sh -c "ghistory diff {}^..{} | grep thing"`
#
# Give the current state of this file as it was 10 days ago
# `ghistory checkout \'master@{7 days ago}\' -- foo.txt`
#
# Restore a previous version of a file
# Reference: https://stackoverflow.com/a/57676960/3626104
# `ghistory restore --source=\'HEAD@{1 week ago}\' foo.txt` # Requires git 2.23+
#
# Reference:
# http://dev.bennage.com/blog/2012/02/01/finding-stuff-in-your-git-repo-2/
# https://dev.to/iggredible/learn-git-grep-to-boost-your-command-line-search-jh7
#
BACKUP_DIRECTORY=~/.vim_custom_backups
# Reference: https://stackoverflow.com/a/21425102/3626104
USER_INPUT=$@
RELATIVE_DIRECTORY=`echo $PWD | sed 's:^[/\\]*::'`
BACKUP_CURRENT_DIRECTORY=$BACKUP_DIRECTORY/$RELATIVE_DIRECTORY
if [ "$USER_INPUT" = "CHANGED" -o "$USER_INPUT" = "CHANGED -- ." ]
then
MODIFIED_INPUT="log --pretty='' --name-only"
if [ "$USER_INPUT" = "CHANGED -- ." ]
then
MODIFIED_INPUT="log --pretty='' --name-only -- ."
fi
echo >&2 "cd $BACKUP_CURRENT_DIRECTORY && git $MODIFIED_INPUT | awk '!seen[$0]++' | tac | sed \"s:^$RELATIVE_DIRECTORY[/\\]*::g\""
eval "cd $BACKUP_CURRENT_DIRECTORY && git $MODIFIED_INPUT" | awk '!seen[$0]++' | tac | sed "s:^$RELATIVE_DIRECTORY[/\\]*::g"
exit 0
fi
# A small "alias" - make ORIGINAL refer to the first commit in the git repository
MODIFIED_INPUT=`echo $USER_INPUT | sed 's:ORIGINAL:\`ghistory rev-list --max-parents=0 HEAD\`:g'`
echo >&2 "cd $BACKUP_CURRENT_DIRECTORY && git $MODIFIED_INPUT"
eval "cd $BACKUP_CURRENT_DIRECTORY && git $MODIFIED_INPUT"