From 5ff38e903a9ada8938aa71695af44b8bdde29fa9 Mon Sep 17 00:00:00 2001 From: p4bl0 Date: Mon, 2 Aug 2010 13:49:17 +0200 Subject: post-receive now works :-) --- README | 99 +++++++++------ build.sh | 2 +- html-gen.sh | 367 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ install.sh | 13 +- post-commit.sh | 364 ------------------------------------------------------- post-receive.sh | 39 +++++- 6 files changed, 476 insertions(+), 408 deletions(-) create mode 100644 html-gen.sh diff --git a/README b/README index 8a3a5f0..5a6494d 100644 --- a/README +++ b/README @@ -1,14 +1,24 @@ -fugitive README file +fugitive: README

Info

+

fugitive is a blog engine running on top of git using hooks to generate static html pages and thus having only git as dependency.

+

+ In its hooks, fugitive uses only standard UNIX® tools that are included in + the GNU core-utils package, plus sh as script interpreter. That's it.
+ Moreover, evrything that can be done using git, is.
+ No dependencies like rack, heroku, or whatever Ruby gems you can think of. No + configuration files. No metadata in your articles files. Hell, if you want to + you could even make a template that use git log as + storage backend, which means no files either, just and only git. +

Install

-

Build

+

Build

If you want to build fugitive from the source, clone the git repository:
@@ -17,14 +27,24 @@ fugitive README file Then go in the newly created directory: cd fugitive, and run the build script: ./build.sh.
- This will generate an executable file "fugitive". + This will generate an executable file "fugitive" which you can use + to create your blog.

-

Create a blog

+

Create a blog

- If you have the "fugitive" executable file and want to start a new - blog: Run fugitive --install <dir>. + There's two install mode for fugitive: local and remote. The local mode + should be used to install a repository where you edit your blog, and the + remote mode for a repository to which you're going to push to publish your + blog.
+ The local mode can also be used to publish if you edit your file directly on + your server. +

+

+ To create you blog run the commande:
+ fugitive --install-mode <dir>, + where mode is either "local" or "remote"
- This will create the git repos with appropriate hooks and files in + This will create the git repos with appropriate hooks, config and files in <dir>.
If <dir> isn't specified then the current working directory is used. @@ -34,24 +54,14 @@ fugitive README file parameter in your git configuration. See configuration for details.

-

- You need to use the same process to install any remote - repository where you'd like to push your blog. -

-

Update

-

- Run fugitive --install-hooks <dir>.
- This will only (re)install fugitive hooks scripts.
- If <dir> isn't specified then the current working directory is used. -

Configuration

All this settings are in the "fugitive" section of the git config. - You can change them with the command git config - fugitive.parameter value, where parameter - is one of the following: + You can change them with the command
+ git config fugitive.parameter value, + where parameter is one of the following:

blog-url
@@ -60,24 +70,22 @@ fugitive README file it as soon as possible since it's required for the RSS feed (and used in the default template's footer). -
public-dir
+
public-dir*
This is the path to the directory that will contain the generated html - files. Defautlt value is ".", the root of the git repository. You - could set it to "blog" for instance if you already have a static - website under your git repos. + files. Default value is "_public". You could set it to + "_public/blog" for instance if you want to have have a website in + "_public" and your blog in "/blog".
-
articles-dir
+
articles-dir*
This is the path where fugitive will look for published articles. Default - value is "_articles". This path is relative to the root of the - git repository, must be in it and must not start with ".". + value is "_articles".
-
templates-dire
+
templates-dire*
This is the path where fugitive will look for templates files. Default - value is "_templates". This path is relative to the root of the - git repository, must be in it and must not start with ".". + value is "_templates".
preproc
@@ -87,23 +95,38 @@ fugitive README file

- You must NOT put a trailing '/' at the end of any of the path. + * Those paths are relative to the root of the git repository, must be in it + and must not start with "." neither have a '/' at the end. Example: + "dir/subdir" is valid but "./dir/subdir" and + "dir/subdir/" are not.

Usage

-

General use

+

General use

Article you want to publish should be file without the .html extension in the - articles-dir directory (see CONFIGURATION). + articles-dir directory (see CONFIGURATION). The first line of the + file will be used as title and the rest of the file as the content of the + article. +

+

+ By default there's a "_drafts" directory in which you can put + articles you are writing and you want to version control in your git + repository but you don't want to publish yet. +

+

+ When you commit, *TODO*: explain the process.

- The first line of the file will be used as title and the rest of the file as - the content. + When you push to a remote repository installed with fugitive, the same thing + will happen but instead of looking only at the last commit, the hooks will + analyse every change since the last push and then (re)generate html files + accordingly.

- DO NOT CREATE AN ARTICLE FILE NAMED "archives".
- DO NOT CREATE AN ARTICLE FILE NAMED "index". + Do not create an article file named "archives".
+ Do not create an article file named "index".

-

Template system

+

Template system

*TODO*

diff --git a/build.sh b/build.sh index 4bbd162..a012ce7 100755 --- a/build.sh +++ b/build.sh @@ -12,7 +12,7 @@ include_file() { cp install.sh tmp1 i=1 -for f in README post-commit.sh post-receive.sh default-files/*; do +for f in README post-commit.sh post-receive.sh html-gen.sh default-files/*; do j=$((1 - i)) include_file tmp$i "$f" > tmp$j i=$j diff --git a/html-gen.sh b/html-gen.sh new file mode 100644 index 0000000..eb26772 --- /dev/null +++ b/html-gen.sh @@ -0,0 +1,367 @@ +blog_url=`git config --get fugitive.blog-url` +if [ "$blog_url" = "" ]; then + echo -n "[fugitive] WARNING: git config fugitive.blog-url is empty and " + echo "should not be, please set it as soon as possible." +fi +templates_dir=`git config --get fugitive.templates-dir` +public_dir=`git config --get fugitive.public-dir` +if [ ! -d "$public_dir" ]; then mkdir -p "$public_dir"; fi +articles_dir=`git config --get fugitive.articles-dir` +preproc=`git config --get fugitive.preproc` + +generated_files=`mktemp --suffix "-fugitive"` + +articles_sorted=`mktemp --suffix "-fugitive"` +for f in "$articles_dir"/*; do + ts=`git log --format="%at" -- "$f" | tail -1` + if [ "$ts" != "" ]; then + echo "$ts ${f#$articles_dir/}" + fi +done | sort -nr | cut -d' ' -f2 > "$articles_sorted" + +articles_sorted_with_delete=`mktemp --suffix "-fugitive"` +for f in "$articles_dir"/* $deleted_files; do + ts=`git log --format="%at" -- "$f" | tail -1` + if [ "$ts" != "" ]; then + echo "$ts ${f#$articles_dir/}" + fi +done | sort -nr | cut -d' ' -f2 > "$articles_sorted_with_delete" + +commits=`mktemp --suffix "-fugitive"` +git log --oneline | cut -d' ' -f1 > "$commits" + +get_article_info() { + git log --format="$1" -- "$articles_dir/$2" +} +get_article_next_file() { + next=`grep -B1 "$1" "$articles_sorted" | head -1` + if [ "$next" != "$1" ]; then + echo "$next" + fi +} +get_article_previous_file() { + previous=`grep -A1 "$1" "$articles_sorted" | tail -1` + if [ "$previous" != "$1" ]; then + echo "$previous" + fi +} +get_deleted_next_file() { + next=`grep -B1 "$1" "$articles_sorted_with_delete" | head -1` + if [ "`echo $deleted_files | grep -c \"$next\"`" = "0" ]; then + echo "$next" + fi +} +get_deleted_previous_file() { + previous=`grep -A1 "$1" "$articles_sorted_with_delete" | tail -1` + if [ "`echo $deleted_files | grep -c \"$previous\"`" = "0" ]; then + echo "$previous" + fi +} +get_article_title() { + if [ "$1" != "" ]; then + head -1 "$articles_dir/$1" + fi +} +get_article_content() { + tmp=`mktemp --suffix "-fugitive"` + tail -n+2 "$articles_dir/$1" > "$tmp" + echo "$tmp" +} + +get_commit_info() { + git show --quiet --format="$1" "$2" +} +get_commit_body() { + tmp=`mktemp --suffix "-fugitive"` + git show --quiet --format="%b" "$1" > "$tmp" + if [ "`cat \"$tmp\" | sed \"/^$/d\" | wc -l`" != "0" ]; then + echo "$tmp" + fi +} + +sanit_mail() { + sed "s/@/[at]/;s/\./(dot)/" +} + +replace_condition() { + if [ "$2" = "" ]; then + sed "s//\n\0\n/g" | \ + sed "//,//bdel + b + :del + s/.*// + //b + d" + else + sed "s///" + fi +} + +replace_str() { + esc=`echo $2 | sed 's/\//\\\\\//g'` + replace_condition "$1" "$2" | \ + sed "s//$esc/g" +} + +# REMEMBER: 2nd arg should be a tempfile! +replace_file() { + if [ -f "$2" ]; then + sed "s//\n\0\n/g" | \ + sed "// { + r $2 + d }" + rm "$2" + else + cat + fi +} + +replace_includes() { + buf=`mktemp --suffix "-fugitive"` + buf2=`mktemp --suffix "-fugitive"` + cat > "$buf" + includes=`cat "$buf" | \ + sed "s//\n\0\n/g" | \ + grep -E "<\?fugitive\s+include:.+\s*\?>" | \ + sed "s/^$//"` + for i in $includes; do + esc=`echo -n $i | sed 's/\//\\\\\//g'` + inc="$templates_dir/$i" + cat "$buf" | \ + sed "// { + r $inc + d }" > "$buf2" + tmpbuf="$buf" + buf="$buf2" + buf2="$tmpbuf" + done + cat "$buf" + rm "$buf" "$buf2" +} + +replace_commit_info() { + commit_Hash=`get_commit_info "%H" "$1"` + commit_hash=`get_commit_info "%h" "$1"` + commit_author=`get_commit_info "%an" "$1"` + commit_author_email=`get_commit_info "%ae" "$1" | sanit_mail` + commit_datetime=`get_commit_info "%ai" "$1"` + commit_date=`echo $commit_datetime | cut -d' ' -f1` + commit_time=`echo $commit_datetime | cut -d' ' -f2` + commit_timestamp=`get_commit_info "%at" "$1"` + commit_subject=`get_commit_info "%s" "$1"` + commit_slug=`get_commit_info "%f" "$1"` + commit_body=`get_commit_body "$1"` + + replace_str "commit_Hash" "$commit_Hash" | \ + replace_str "commit_hash" "$commit_hash" | \ + replace_str "commit_author" "$commit_author" | \ + replace_str "commit_author_email" "$commit_author_email" | \ + replace_str "commit_datetime" "$commit_datetime" | \ + replace_str "commit_date" "$commit_date" | \ + replace_str "commit_time" "$commit_time" | \ + replace_str "commit_timestamp" "$commit_timestamp" | \ + replace_str "commit_subject" "$commit_subject" | \ + replace_str "commit_slug" "$commit_slug" | \ + replace_file "commit_body" "$commit_body" +} + +replace_article_info() { + article_title=`get_article_title "$1"` + article_cdatetime=`get_article_info "%ai" "$1" | tail -1` + article_cdate=`echo "$article_cdatetime" | cut -d' ' -f1` + article_ctime=`echo "$article_cdatetime" | cut -d' ' -f2` + article_ctimestamp=`get_article_info "%at" "$1" | tail -1` + u=`get_article_info "%ai" "$1" | wc -l` + article_mdatetime=`if test "$u" -gt 1; then get_article_info "%ai" "$1" | \ + head -1; fi` + article_mdate=`echo "$article_mdatetime" | cut -d' ' -f1` + article_mtime=`echo "$article_mdatetime" | cut -d' ' -f2` + article_mtimestamp=`if test "$u" -gt 1; then get_article_info "%at" \ + "$1" | head -1; fi` + article_cauthor=`get_article_info "%an" "$1" | tail -1` + article_cauthor_email=`get_article_info "%ae" "$1" | tail -1 | sanit_mail` + article_mauthor=`get_article_info "%an" "$1" | head -1` + article_mauthor_email=`get_article_info "%ae" "$1" | head -1 | sanit_mail` + article_previous_file=`get_article_previous_file "$1"` + article_previous_title=`get_article_title "$article_previous_file"` + article_next_file=`get_article_next_file "$1"` + article_next_title=`get_article_title "$article_next_file"` + + replace_file "article_content" "`get_article_content \"$1\"`" | \ + replace_str "article_file" "$1" | \ + replace_str "article_title" "$article_title" | \ + replace_str "article_cdatetime" "$article_cdatetime" | \ + replace_str "article_cdate" "$article_cdate" | \ + replace_str "article_ctime" "$article_ctime" | \ + replace_str "article_ctimestamp" "$article_ctimestamp" | \ + replace_str "article_mdatetime" "$article_mdatetime" | \ + replace_str "article_mdate" "$article_mdate" | \ + replace_str "article_mtime" "$article_mtime" | \ + replace_str "article_mtimestamp" "$article_mtimestamp" | \ + replace_str "article_cauthor" "$article_cauthor" | \ + replace_str "article_cauthor_email" "$article_cauthor_email" | \ + replace_str "article_mauthor" "$article_mauthor" | \ + replace_str "article_mauthor_email" "$article_mauthor_email" | \ + replace_str "article_previous_file" "$article_previous_file" | \ + replace_str "article_previous_title" "$article_previous_title" | \ + replace_str "article_next_file" "$article_next_file" | \ + replace_str "article_next_title" "$article_next_title" +} + +replace_empty_article_info() { + replace_str "article_file" "" | \ + replace_str "article_title" "" | \ + replace_str "article_cdatetime" "" | \ + replace_str "article_cdate" "" | \ + replace_str "article_ctime" "" | \ + replace_str "article_ctimestamp" "" | \ + replace_str "article_mdatetime" "" | \ + replace_str "article_mdate" "" | \ + replace_str "article_mtime" "" | \ + replace_str "article_mtimestamp" "" | \ + replace_str "article_cauthor" "" | \ + replace_str "article_cauthor_email" "" | \ + replace_str "article_mauthor" "" | \ + replace_str "article_mauthor_email" "" | \ + replace_str "article_previous_file" "" | \ + replace_str "article_previous_title" "" | \ + replace_str "article_next_file" "" | \ + replace_str "article_next_title" "" +} + +replace_foreach () { + foreach_body=`mktemp --suffix "-fugitive"` + tmpfile=`mktemp --suffix "-fugitive"` + temp=`mktemp --suffix "-fugitive"` + fe="foreach:$1" + cat > "$temp" + cat "$temp" | \ + sed -n "//,//p" | \ + tail -n +2 | head -n -1 > "$foreach_body" + if [ ! -s "$foreach_body" ]; then + cat "$temp" + rm "$foreach_body" "$tmpfile" "$temp" + return + fi + cat "$temp" | \ + sed "s//\n\0/" | \ + sed "//,//d" | \ + cat > "$tmpfile" + for i in `cat "$2"`; do + cat "$foreach_body" | replace_$1_info "$i" + done > "$temp" + cat "$tmpfile" | replace_file "foreach_body" "$temp" + rm "$foreach_body" "$tmpfile" +} + +generate_article() { + if [ "$preproc" != "" ]; then + preproc_bak=`mktemp --suffix "-fugitive" -d "$articles_dir"` + mv "$1" "$preproc_bak" + ($preproc) < "$preproc_bak" > "$1" + fi + art="${1#$articles_dir/}" + cat "$templates_dir/article.html" | \ + replace_includes | \ + replace_str "page_title" "`get_article_title \"$art\"`" | \ + replace_str "blog_url" "$blog_url" | \ + replace_commit_info "-1" | \ + replace_article_info "$art" | \ + sed "/^\s*$/d" > "$public_dir/$art.html" + if [ "$preproc" != "" ]; then mv "$preproc_bak" "$1"; fi +} + +regenerate_previous_and_next_article_maybe() { + if [ "$1" != "" -a \ + "`grep -c \"$1\" \"$generated_files\"`" = "0" ]; then + echo -n "[fugitive] Regenerating $public_dir/$1.html" + echo -n " (as previous article) from $articles_dir/$1... " + generate_article "$articles_dir/$1" + echo "done." + echo "$1" >> "$generated_files" + fi + if [ "$2" != "" -a \ + "`grep -c \"$2\" \"$generated_files\"`" = "0" ]; then + echo -n "[fugitive] Regenerating $public_dir/$2.html" + echo -n " (as next article) from $articles_dir/$2... " + generate_article "$articles_dir/$2" + echo "done." + echo "$2" >> "$generated_files" + fi +} + +modification=0 + +for f in $added_files $modified_files; do + if [ "$f" != "${f#$articles_dir}" ]; then + modification=$((modification + 1)) + echo -n "[fugitive] Generating $public_dir/${f#$articles_dir/}.html from" + echo -n " $f... " + generate_article "$f" + echo "done." + echo "${f#$articles_dir/}" >> "$generated_files" + fi +done + +for f in $added_files; do + if [ "$f" != "${f#$articles_dir}" ]; then + art="${f#$articles_dir/}" + echo -n "[fugitive] Adding $public_dir/$art.html to git ignore list... " + echo "$public_dir/$art.html" >> .git/info/exclude + echo "done." + previous=`get_article_previous_file "$art"` + next=`get_article_next_file "$art"` + regenerate_previous_and_next_article_maybe "$previous" "$next" + fi +done + +for f in $deleted_files; do + if [ "$f" != "${f#$articles_dir}" ]; then + modification=$((modification + 1)) + art="${f#$articles_dir/}" + echo -n "[fugitive] Deleting $public_dir/$art.html... " + rm "$public_dir/$art.html" + echo "done." + echo -n "[fugitive] Removing $art.html from git ignore list... " + sed -i "/^$public_dir\/$art.html$/d" .git/info/exclude + echo "done." + previous=`get_deleted_previous_file "$art"` + next=`get_deleted_next_file "$art"` + regenerate_previous_and_next_article_maybe "$previous" "$next" + fi +done + +if [ $modification -gt 0 ]; then + echo -n "[fugitive] Generating $public_dir/archives.html... " + cat "$templates_dir/archives.html" | \ + replace_includes | \ + replace_foreach "article" "$articles_sorted" | \ + replace_foreach "commit" "$commits" | \ + replace_empty_article_info | \ + replace_str "page_title" "archives" | \ + replace_str "blog_url" "$blog_url" | \ + replace_commit_info "-1" | \ + sed "/^\s*$/d" > "$public_dir/archives.html" + echo "done." + echo -n "[fugitive] Generating $public_dir/feed.xml... " + last_5_articles=`mktemp --suffix "-fugitive"` + head -5 "$articles_sorted" > "$last_5_articles" + last_5_commits=`mktemp --suffix "-fugitive"` + head -5 "$commits" > "$last_5_commits" + cat "$templates_dir/feed.xml" | \ + replace_foreach "article" "$last_5_articles" | \ + replace_foreach "commit" "$last_5_commits" | \ + replace_str "blog_url" "$blog_url" | \ + replace_commit_info "-1" | \ + sed "/^\s*$/d" > "$public_dir/feed.xml" + echo "done." + rm "$last_5_articles" "$last_5_commits" + echo -n "[fugitive] Using last published article as index page... " + cp "$public_dir/`head -1 $articles_sorted`.html" "$public_dir/index.html" + echo "done". + echo "[fugitive] Blog update complete." +fi +rm "$articles_sorted" +rm "$articles_sorted_with_delete" +rm "$commits" +rm "$generated_files" diff --git a/install.sh b/install.sh index 758a49f..476515e 100644 --- a/install.sh +++ b/install.sh @@ -15,10 +15,14 @@ fugitive_install_hooks() { (base64 -d | gunzip) > .git/hooks/post-commit < .git/hooks/post-receive <> .git/hooks/post-receive </dev/null - git commit -m "fugitive inital import" >/dev/null + git commit -m "fugitive inital import" &>/dev/null echo "done." echo "Writing dummy article (README) and adding it to the repos... " (base64 -d | gunzip) > _articles/README </dev/null echo "done." fi + echo "Installation complete, please set your blog url using" + echo '`git config fugitive.blog-url ""`.' cd - >/dev/null - echo 'Installation almost complete, please visit your blog :-).' } case "$1" in - "--help") fugitive_help >&2;; + "--help"|"-h") fugitive_help >&2;; "--install"|"--install-local") fugitive_install "$2" "local";; "--install-remote") fugitive_install "$2" "remote";; "--install-hooks") fugitive_install_hooks "$2";; diff --git a/post-commit.sh b/post-commit.sh index 044526f..01e755c 100644 --- a/post-commit.sh +++ b/post-commit.sh @@ -6,367 +6,3 @@ 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` - -blog_url=`git config --get fugitive.blog-url` -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` -preproc=`git config --get fugitive.preproc` - -generated_files=`mktemp --suffix "-fugitive"` - -articles_sorted=`mktemp --suffix "-fugitive"` -for f in $articles_dir/*; do - ts=`git log --format="%at" -- "$f" | tail -1` - if [ "$ts" != "" ]; then - echo "$ts ${f#$articles_dir/}" - fi -done | sort -nr | cut -d' ' -f2 > "$articles_sorted" - -articles_sorted_with_delete=`mktemp --suffix "-fugitive"` -for f in $articles_dir/* $deleted_files; do - ts=`git log --format="%at" -- "$f" | tail -1` - if [ "$ts" != "" ]; then - echo "$ts ${f#$articles_dir/}" - fi -done | sort -nr | cut -d' ' -f2 > "$articles_sorted_with_delete" - -commits=`mktemp --suffix "-fugitive"` -git log --oneline | cut -d' ' -f1 > "$commits" - -get_article_info() { - git log --format="$1" -- "$articles_dir/$2" -} -get_article_next_file() { - next=`grep -B1 "$1" "$articles_sorted" | head -1` - if [ "$next" != "$1" ]; then - echo "$next" - fi -} -get_article_previous_file() { - previous=`grep -A1 "$1" "$articles_sorted" | tail -1` - if [ "$previous" != "$1" ]; then - echo "$previous" - fi -} -get_deleted_next_file() { - next=`grep -B1 "$1" "$articles_sorted_with_delete" | head -1` - if [ "`echo $deleted_files | grep -c \"$next\"`" = "0" ]; then - echo "$next" - fi -} -get_deleted_previous_file() { - previous=`grep -A1 "$1" "$articles_sorted_with_delete" | tail -1` - if [ "`echo $deleted_files | grep -c \"$previous\"`" = "0" ]; then - echo "$previous" - fi -} -get_article_title() { - if [ "$1" != "" ]; then - head -1 "$articles_dir/$1" - fi -} -get_article_content() { - tmp=`mktemp --suffix "-fugitive"` - tail -n+2 "$articles_dir/$1" > "$tmp" - echo "$tmp" -} - -get_commit_info() { - git show --quiet --format="$1" "$2" -} -get_commit_body() { - tmp=`mktemp --suffix "-fugitive"` - git show --quiet --format="%b" "$1" > "$tmp" - if [ "`cat \"$tmp\" | sed \"/^$/d\" | wc -l`" != "0" ]; then - echo "$tmp" - fi -} - -sanit_mail() { - sed "s/@/[at]/;s/\./(dot)/" -} - -replace_condition() { - if [ "$2" = "" ]; then - sed "s//\n\0\n/g" | \ - sed "//,//bdel - b - :del - s/.*// - //b - d" - else - sed "s///" - fi -} - -replace_str() { - esc=`echo $2 | sed 's/\//\\\\\//g'` - replace_condition "$1" "$2" | \ - sed "s//$esc/g" -} - -# REMEMBER: 2nd arg should be a tempfile! -replace_file() { - if [ -f "$2" ]; then - sed "s//\n\0\n/g" | \ - sed "// { - r $2 - d }" - rm "$2" - else - cat - fi -} - -replace_includes() { - buf=`mktemp --suffix "-fugitive"` - buf2=`mktemp --suffix "-fugitive"` - cat > "$buf" - includes=`cat "$buf" | \ - sed "s//\n\0\n/g" | \ - grep -E "<\?fugitive\s+include:.+\s*\?>" | \ - sed "s/^$//"` - for i in $includes; do - esc=`echo -n $i | sed 's/\//\\\\\//g'` - inc="$templates_dir/$i" - cat "$buf" | \ - sed "// { - r $inc - d }" > "$buf2" - tmpbuf="$buf" - buf="$buf2" - buf2="$tmpbuf" - done - cat "$buf" - rm "$buf" "$buf2" -} - -replace_commit_info() { - commit_Hash=`get_commit_info "%H" "$1"` - commit_hash=`get_commit_info "%h" "$1"` - commit_author=`get_commit_info "%an" "$1"` - commit_author_email=`get_commit_info "%ae" "$1" | sanit_mail` - commit_datetime=`get_commit_info "%ai" "$1"` - commit_date=`echo $commit_datetime | cut -d' ' -f1` - commit_time=`echo $commit_datetime | cut -d' ' -f2` - commit_timestamp=`get_commit_info "%at" "$1"` - commit_subject=`get_commit_info "%s" "$1"` - commit_slug=`get_commit_info "%f" "$1"` - commit_body=`get_commit_body "$1"` - - replace_str "commit_Hash" "$commit_Hash" | \ - replace_str "commit_hash" "$commit_hash" | \ - replace_str "commit_author" "$commit_author" | \ - replace_str "commit_author_email" "$commit_author_email" | \ - replace_str "commit_datetime" "$commit_datetime" | \ - replace_str "commit_date" "$commit_date" | \ - replace_str "commit_time" "$commit_time" | \ - replace_str "commit_timestamp" "$commit_timestamp" | \ - replace_str "commit_subject" "$commit_subject" | \ - replace_str "commit_slug" "$commit_slug" | \ - replace_file "commit_body" "$commit_body" -} - -replace_article_info() { - article_title=`get_article_title "$1"` - article_cdatetime=`get_article_info "%ai" "$1" | tail -1` - article_cdate=`echo "$article_cdatetime" | cut -d' ' -f1` - article_ctime=`echo "$article_cdatetime" | cut -d' ' -f2` - article_ctimestamp=`get_article_info "%at" "$1" | tail -1` - u=`get_article_info "%ai" "$1" | wc -l` - article_mdatetime=`if test "$u" -gt 1; then get_article_info "%ai" "$1" | \ - head -1; fi` - article_mdate=`echo "$article_mdatetime" | cut -d' ' -f1` - article_mtime=`echo "$article_mdatetime" | cut -d' ' -f2` - article_mtimestamp=`if test "$u" -gt 1; then get_article_info "%at" \ - "$1" | head -1; fi` - article_cauthor=`get_article_info "%an" "$1" | tail -1` - article_cauthor_email=`get_article_info "%ae" "$1" | tail -1 | sanit_mail` - article_mauthor=`get_article_info "%an" "$1" | head -1` - article_mauthor_email=`get_article_info "%ae" "$1" | head -1 | sanit_mail` - article_previous_file=`get_article_previous_file "$1"` - article_previous_title=`get_article_title "$article_previous_file"` - article_next_file=`get_article_next_file "$1"` - article_next_title=`get_article_title "$article_next_file"` - - replace_file "article_content" "`get_article_content \"$1\"`" | \ - replace_str "article_file" "$1" | \ - replace_str "article_title" "$article_title" | \ - replace_str "article_cdatetime" "$article_cdatetime" | \ - replace_str "article_cdate" "$article_cdate" | \ - replace_str "article_ctime" "$article_ctime" | \ - replace_str "article_ctimestamp" "$article_ctimestamp" | \ - replace_str "article_mdatetime" "$article_mdatetime" | \ - replace_str "article_mdate" "$article_mdate" | \ - replace_str "article_mtime" "$article_mtime" | \ - replace_str "article_mtimestamp" "$article_mtimestamp" | \ - replace_str "article_cauthor" "$article_cauthor" | \ - replace_str "article_cauthor_email" "$article_cauthor_email" | \ - replace_str "article_mauthor" "$article_mauthor" | \ - replace_str "article_mauthor_email" "$article_mauthor_email" | \ - replace_str "article_previous_file" "$article_previous_file" | \ - replace_str "article_previous_title" "$article_previous_title" | \ - replace_str "article_next_file" "$article_next_file" | \ - replace_str "article_next_title" "$article_next_title" -} - -replace_empty_article_info() { - replace_str "article_file" "" | \ - replace_str "article_title" "" | \ - replace_str "article_cdatetime" "" | \ - replace_str "article_cdate" "" | \ - replace_str "article_ctime" "" | \ - replace_str "article_ctimestamp" "" | \ - replace_str "article_mdatetime" "" | \ - replace_str "article_mdate" "" | \ - replace_str "article_mtime" "" | \ - replace_str "article_mtimestamp" "" | \ - replace_str "article_cauthor" "" | \ - replace_str "article_cauthor_email" "" | \ - replace_str "article_mauthor" "" | \ - replace_str "article_mauthor_email" "" | \ - replace_str "article_previous_file" "" | \ - replace_str "article_previous_title" "" | \ - replace_str "article_next_file" "" | \ - replace_str "article_next_title" "" -} - -replace_foreach () { - foreach_body=`mktemp --suffix "-fugitive"` - tmpfile=`mktemp --suffix "-fugitive"` - temp=`mktemp --suffix "-fugitive"` - fe="foreach:$1" - cat > "$temp" - cat "$temp" | \ - sed -n "//,//p" | \ - tail -n +2 | head -n -1 > "$foreach_body" - if [ ! -s "$foreach_body" ]; then - cat "$temp" - rm "$foreach_body" "$tmpfile" "$temp" - return - fi - cat "$temp" | \ - sed "s//\n\0/" | \ - sed "//,//d" | \ - cat > "$tmpfile" - for i in `cat "$2"`; do - cat "$foreach_body" | replace_$1_info "$i" - done > "$temp" - cat "$tmpfile" | replace_file "foreach_body" "$temp" - rm "$foreach_body" "$tmpfile" -} - -generate_article() { - if [ "$preproc" != "" ]; then - preproc_bak=`mktemp --suffix "-fugitive" -d "$articles_dir"` - mv "$1" "$preproc_bak" - ($preproc) < "$preproc_bak" > "$1" - fi - art="${1#$articles_dir/}" - cat "$templates_dir/article.html" | \ - replace_includes | \ - replace_str "page_title" "`get_article_title \"$art\"`" | \ - replace_str "blog_url" "$blog_url" | \ - replace_commit_info "-1" | \ - replace_article_info "$art" | \ - sed "/^\s*$/d" > "$public_dir/$art.html" - if [ "$preproc" != "" ]; then mv "$preproc_bak" "$1"; fi -} - -regenerate_previous_and_next_article_maybe() { - if [ "$1" != "" -a \ - "`grep -c \"$1\" \"$generated_files\"`" = "0" ]; then - echo -n "[fugitive] Regenerating $public_dir/$1.html" - echo -n " (as previous article) from $articles_dir/$1... " - generate_article "$articles_dir/$1" - echo "done." - echo "$1" >> "$generated_files" - fi - if [ "$2" != "" -a \ - "`grep -c \"$2\" \"$generated_files\"`" = "0" ]; then - echo -n "[fugitive] Regenerating $public_dir/$2.html" - echo -n " (as next article) from $articles_dir/$2... " - generate_article "$articles_dir/$2" - echo "done." - echo "$2" >> "$generated_files" - fi -} - -modification=0 - -for f in $added_files $modified_files; do - if [ "$f" != "${f#$articles_dir}" ]; then - modification=$((modification + 1)) - echo -n "[fugitive] Generating $public_dir/${f#$articles_dir/}.html from" - echo -n " $f... " - generate_article "$f" - echo "done." - echo "${f#$articles_dir/}" >> "$generated_files" - fi -done - -for f in $added_files; do - if [ "$f" != "${f#$articles_dir}" ]; then - art="${f#$articles_dir/}" - echo -n "[fugitive] Adding $public_dir/$art.html to git ignore list... " - echo "$public_dir/$art.html" >> .git/info/exclude - echo "done." - previous=`get_article_previous_file "$art"` - next=`get_article_next_file "$art"` - regenerate_previous_and_next_article_maybe "$previous" "$next" - fi -done - -for f in $deleted_files; do - if [ "$f" != "${f#$articles_dir}" ]; then - modification=$((modification + 1)) - art="${f#$articles_dir/}" - echo -n "[fugitive] Deleting $public_dir/$art.html... " - rm "$public_dir/$art.html" - echo "done." - echo -n "[fugitive] Removing $art.html from git ignore list... " - sed -i "/^$art.html$/d" .git/info/exclude - echo "done." - previous=`get_deleted_previous_file "$art"` - next=`get_deleted_next_file "$art"` - regenerate_previous_and_next_article_maybe "$previous" "$next" - fi -done - -if [ $modification -gt 0 ]; then - echo -n "[fugitive] Generating $public_dir/archives.html... " - cat "$templates_dir/archives.html" | \ - replace_includes | \ - replace_foreach "article" "$articles_sorted" | \ - replace_foreach "commit" "$commits" | \ - replace_empty_article_info | \ - replace_str "page_title" "archives" | \ - replace_str "blog_url" "$blog_url" | \ - replace_commit_info "-1" | \ - sed "/^\s*$/d" > "$public_dir/archives.html" - echo "done." - echo -n "[fugitive] Generating $public_dir/feed.xml... " - last_5_articles=`mktemp --suffix "-fugitive"` - head -5 "$articles_sorted" > "$last_5_articles" - last_5_commits=`mktemp --suffix "-fugitive"` - head -5 "$commits" > "$last_5_commits" - cat "$templates_dir/feed.xml" | \ - replace_foreach "article" "$last_5_articles" | \ - replace_foreach "commit" "$last_5_commits" | \ - replace_str "blog_url" "$blog_url" | \ - replace_commit_info "-1" | \ - sed "/^\s*$/d" > "$public_dir/feed.xml" - echo "done." - rm "$last_5_articles" "$last_5_commits" - echo -n "[fugitive] Using last published article as index page... " - cp "$public_dir/`head -1 $articles_sorted`.html" "$public_dir/index.html" - echo "done". - echo "[fugitive] Blog update complete." -fi -rm "$articles_sorted" -rm "$articles_sorted_with_delete" -rm "$commits" -rm "$generated_files" diff --git a/post-receive.sh b/post-receive.sh index ba901c5..acec2b6 100644 --- a/post-receive.sh +++ b/post-receive.sh @@ -1,3 +1,40 @@ #!/bin/sh -echo "hi!" +cd .. +export GIT_DIR=.git +git reset --hard + +refs=`cat - | head -1 | cut -d' ' -f1,2` +ref_begin=`echo $refs | cut -d' ' -f1` +ref_end=`echo $refs | cut -d' ' -f2` + +if [ "$ref_begin" = "0000000000000000000000000000000000000000" ]; then + range="" # first push, empty repos. +else + range="$ref_begin..$ref_end" +fi + +articles_dir=`git config --get fugitive.articles-dir` + +added_files=`git log $range --name-status --pretty="format:" | \ + grep -E '^A' | cut -f2 | sort | uniq` +modified_files=`git log $range --name-status --pretty="format:" | \ + grep -E '^M' | cut -f2 | sort | uniq` +deleted_files=`git log $range --name-status --pretty="format:" | \ + grep -E '^D' | cut -f2 | sort | uniq` + +tmpart=`mktemp --suffix "-fugitive"` +tmpadd=`mktemp --suffix "-fugitive"` +tmpmod=`mktemp --suffix "-fugitive"` +tmpdel=`mktemp --suffix "-fugitive"` +ls "$articles_dir"/* > "$tmpart" +echo "$added_files" | tr " " "\n" > "$tmpadd" +echo "$modified_files" | tr " " "\n" > "$tmpmod" +echo "$deleted_files" | tr " " "\n" > "$tmpdel" +deleted_files=`comm -23 --nocheck-order "$tmpdel" "$tmpart"` +echo "$deleted_files" | tr " " "\n" > "$tmpdel" +deleted_files=`comm -23 --nocheck-order "$tmpdel" "$tmpadd"` +added_files=`comm -12 --nocheck-order "$tmpadd" "$tmpart"` +echo "$added_files" | tr " " "\n" > "$tmpadd" +modified_files=`comm -23 --nocheck-order "$tmpmod" "$tmpadd"` +rm "$tmpart" "$tmpadd" "$tmpmod" "$tmpdel" -- cgit v1.2.3