summaryrefslogtreecommitdiff
path: root/post-commit.sh
blob: c028f85d63dda8ef09bd115597ffeb5e84bc4b3c (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/sh

public_dir=`git config --get fugitive.public-dir`
if [ ! -d "$public_dir" ]; then mkdir -p "$public_dir"; fi
templates_dir=`git config --get fugitive.templates-dir`
articles_dir=`git config --get fugitive.articles-dir`

added_files=`git log -1 --name-status --pretty="format:" | grep -E '^A' | \
  cut -f2`
modified_files=`git log -1 --name-status --pretty="format:" | grep -E '^M' | \
  cut -f2`
deleted_files=`git log -1 --name-status --pretty="format:" | grep -E '^D' | \
  cut -f2`

last_published_article=`git log --name-status --pretty="format:" | \
  grep -E '^A' | cut -f2 | grep -E '^$articles_dir' | head -1`

sanit_mail() {
  sed "s/@/[at]/;s/\./(dot)/"
}

commit_Hash=`git log -1 --format="%H"`
commit_hash=`git log -1 --format="%h"`
commit_author=`git log -1 --format="%an"`
commit_author_email=`git log -1 --format="%ae" | sanit_mail`
commit_datetime=`git log -1 --format="%ai"`
commit_date=`git log -1 --format="%ad" --date="short"`
commit_time=`git log -1 --format="%ai" | cut -d' ' -f2`
commit_timestamp=`git log -1 --format="%at"`
commit_subject=`git log -1 --format="%s"`
commit_slug=`git log -1 --format="%f"`
commit_body() {
  tmp=`tempfile -p "fugitive"`
  git log -1 --format="%b" > "$tmp"
  (sleep 5 && rm -f "$tmp") & # this message will self-destruct in 5s
  echo "$tmp"
}

article_info() {
  git log --format="$1" -- "$2"
}
article_title() {
  head -1 "$1"
}
article_content() {
  tmp=`tempfile -p "fugitive"`
  tail -n+2 "$1" > "$tmp"
  (sleep 5 && rm -f "$tmp") & # this message will self-destruct in 5s
  echo "$tmp"
}

replace_var_by_string() {
  sed "s/<?fugitive\s\+$1\s*?>/$2/"
}
replace_var_by_file() {
  sed "/<?fugitive\s\+$1\s*?>/ {
    r $2
    d }"
}
replace_commit_info() {
  replace_var_by_string "commit_Hash" "$commit_Hash" | \
    replace_var_by_string "commit_hash" "$commit_hash" | \
    replace_var_by_string "commit_author" "$commit_author" | \
    replace_var_by_string "commit_author_email" "$commit_author_email" | \
    replace_var_by_string "commit_datetime" "$commit_datetime" | \
    replace_var_by_string "commit_date" "$commit_date" | \
    replace_var_by_string "commit_time" "$commit_time" | \
    replace_var_by_string "commit_timestamp" "$commit_timestamp" | \
    replace_var_by_string "commit_subject" "$commit_subject" | \
    replace_var_by_string "commit_slug" "$commit_slug" | \
    replace_var_by_file "commit_body" "`commit_body`"
}
replace_article_info() {
  cdt=`article_info "%ai" "$1" | tail -1`
  mdt=`article_info "%ai" "$1" | head -1`
  replace_var_by_file "article_content" "`article_content \"$1\"`" | \
    replace_var_by_string "article_title" "`article_title \"$1\"`" | \
    replace_var_by_string "article_cdatetime" "$cdt" | \
    replace_var_by_string "article_cdate" "`echo $cdt | cut -d' ' -f1`" | \
    replace_var_by_string "article_ctime" "`echo $cdt | cut -d' ' -f2`" | \
    replace_var_by_string "article_ctimestamp" \
      "`article_info \"%at\" \"$1\" | tail -1`" | \
    replace_var_by_string "article_mdatetime" "$mdt" | \
    replace_var_by_string "article_mdate" "`echo $mdt | cut -d' ' -f1`" | \
    replace_var_by_string "article_mtime" "`echo $mdt | cut -d' ' -f2`" | \
    replace_var_by_string "article_mtimestamp" \
      "`article_info \"%at\" \"$1\" | head -1`" | \
    replace_var_by_string "article_cauthor" \
      "`article_info \"%an\" \"$1\" | tail -1`" | \
    replace_var_by_string "article_cauthor_email" \
      "`article_info \"%ae\" \"$1\" | tail -1 | sanit_mail`" | \
    replace_var_by_string "article_mauthor" \
      "`article_info \"%an\" \"$1\" | head -1`" | \
    replace_var_by_string "article_mauthor_email" \
      "`article_info \"%ae\" \"$1\" | head -1 | sanit_mail`"
}

for f in $deleted_files; do
  if [ "$f" != "${f#$articles_dir}" ]; then
    echo -n "Deleting $public_dir/${f#$articles_dir/}.html... "
    rm $public_dir/${f#$articles_dir/}.html
    echo "done."
  fi
done

for f in $added_files $modified_files; do
  if [ "$f" != "${f#$articles_dir}" ]; then
    echo -n "Generating $public_dir/${f#$articles_dir/}.html from $f... "
    cat $templates_dir/article.html | \
      replace_commit_info | \
      replace_article_info "$f" | \
      cat > $public_dir/${f#$articles_dir/}.html
    echo "done."
  fi
done