Przeglądaj źródła

Adding orgwatch.sh and related

George Jones 7 miesięcy temu
rodzic
commit
a896618b96
3 zmienionych plików z 115 dodań i 0 usunięć
  1. 53 0
      bin/autocommit.sh
  2. 53 0
      bin/orglinks.sh
  3. 9 0
      bin/orgwatch.sh

+ 53 - 0
bin/autocommit.sh

@@ -0,0 +1,53 @@
+#! /bin/bash
+# Do automatic git-commit to any changes in a diredtory
+#
+# This is intended to be used with "orglinks.sh" which creates
+# hard links for all *.org files under, e.g., $HOME to a
+# parcicular directory, e.g. ~/orgfiles
+#
+# Usage: autocommit DIR
+
+# This is bash, be safe
+set -u
+set -e
+
+# pull in my error, warning, etc.
+source ~/lib/bash/bashutils.sh
+
+# Directory provided as an argument
+dir="${1:-$HOME/orgfiles}"
+
+# Check if the provided directory exists
+if [ ! -d "$dir" ]; then
+  error "Error: Directory '$dir' does not exist."
+fi
+
+cd $dir
+
+# make sure it is a git root dir
+
+if [ -d $dir/.git ]; then
+    :
+else
+    info initializing git in $dir
+    git init .
+fi
+
+# Add all org files
+info adding all .org files
+git add *.org
+
+# get rid of anytthing that's not an .org file
+git clean -f
+
+# Git commit changes if any
+if [[ $(git status --porcelain) ]]; then
+    # Changes exist, so commit them
+    info files changed:
+    git ls-files -m
+    git commit -am "Autocommit `date`"
+    info "Changes committed."
+else
+    # No changes to commit
+    info "No changes to commit."
+fi

+ 53 - 0
bin/orglinks.sh

@@ -0,0 +1,53 @@
+#!/bin/bash
+#
+# ChatGPT Promt:
+#
+# write me a bash script that finds all the .org files under $HOME and
+# hard links them to ~/orgfiles/, changing the "/" in paths to "__"
+
+# This is bash, be safe
+set -u
+set -e
+
+# use my info,warn,error aliases
+source ~/lib/bash/bashutils.sh
+
+# Define source and destination directories
+source_dir="$HOME"
+destination_dir="$HOME/orgfiles"
+
+# Max hard links to make.  If I have more than 10k org files, there are problems.
+MAXFILES=10000
+
+# Create the destination directory if it doesn't exist
+mkdir -p "$destination_dir"
+
+# Find all .org files under $HOME and loop through them
+find "$source_dir" -type f -name "*.org" | grep -v "$destination_dir" | head -$MAXFILES | while read -r org_file; do
+    # Get the relative path by removing the source directory part
+    relative_path="${org_file#$source_dir/}"
+
+    # Replace "/" in paths with "__"
+    modified_relative_path1="${relative_path//\//__}"
+
+    # Replace " " in paths with "_"
+    modified_relative_path="${modified_relative_path1// /_}"
+
+
+    # Construct the destination path
+    destination_path="$destination_dir/$modified_relative_path"
+
+    # if the target file exists, continue
+
+    if [ -f $destination_path ] ; then
+        continue
+    fi
+
+    # Attempt to create a hard link to the destination
+    if ln "$org_file" "$destination_path" 2>/dev/null; then
+        info "Linked file: $org_file -> $destination_path"
+    else
+        warn "Failed to create a hard link for $org_file to $destination_path"
+    fi
+
+done

+ 9 - 0
bin/orgwatch.sh

@@ -0,0 +1,9 @@
+#! /bin/bash
+# link new .org files to ~/orgfiles and do git commint
+#
+# This should be run out of cron periodically
+#
+
+echo "--- Orgwatch `date` ---"
+/home/gmj/bin/orglinks.sh
+/home/gmj/bin/autocommit.sh