Skip to content

Instantly share code, notes, and snippets.

@holodnak
Created July 24, 2013 18:53
Show Gist options
  • Save holodnak/6073382 to your computer and use it in GitHub Desktop.
Save holodnak/6073382 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Simple bash/expect script to manually upload files tracked by git.
# Written by James Holodnak -- https://github.com/holodnak
# extract parent directories from a full path
function mkdirp() {
# initialize the output variable
DIR=""
# loop thru the path starting at the first directory
for d in $(echo $1 | tr "/" "\n"); do
# if this is the first one then just copy
if [ -z $DIR ]; then
DIR="$d"
# prepend a directory seperator, building the path
else
DIR="$DIR/$d"
fi
# output the directory so far
echo "$DIR"
done
}
# check command line arguments
if [ -z $2 ]; then
echo "Usage: $0 <host> <password>"
exit 1
fi
# get list of files tracked by git
FILES=`git ls-files`
# generate list of directories to create
for i in $FILES; do
# get directory that holds this file
DIRNAME=$(dirname $i)
# make sure this file is contained in a directory
if [ "$DIRNAME" != "." ]; then
DIRECTORIES="$DIRECTORIES $DIRNAME"
fi
done
# now sort all directories and remove duplicates (first pass)
DIRECTORIES=$(echo "$DIRECTORIES" | tr ' ' '\n' | sort -u | tr '\n' ' ')
# get the directory trees
DIRS=""
for i in $DIRECTORIES; do
DIRS="$DIRS $(mkdirp $i)"
done
# TODO: FIX THIS
# convert all space-delimeters to newlines
DIRS=$(echo "$DIRS" | tr " " "\n")
# sort all directories and remove duplicates (second pass)
DIRS=$(echo "$DIRS" | tr ' ' '\n' | sort -u | tr '\n' ' ')
# setup username/password/host variables
HOSTNAME=$1
PASSWORD=$2
# run expect to login and perform commands
expect - <<END
set timeout -1
spawn sftp -r $HOSTNAME
expect assword:
send "$PASSWORD\r"
foreach xx { $DIRS } {
expect sftp>
send "mkdir \$xx\r"
}
foreach xx { $FILES } {
expect sftp>
send "put \$xx \$xx\r"
}
expect sftp>
send "quit\r"
expect eof
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment