Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validates problem. Checks for missing files. Prompts to confirm submi… #1

Merged
merged 4 commits into from
Sep 18, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Validates problem. Checks for missing files. Prompts to confirm submi…
…ssion.
  • Loading branch information
dmalan committed Sep 18, 2016
commit c1a7d18215167b4d1bf7ed4f766db05cd56e05e3
87 changes: 78 additions & 9 deletions opt/cs50/submit50/bin/submit50
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,44 @@ for cmd in curl expect jq sed; do
fi
done

# TODO: check if problem exists by querying for .gitignore
# ensure problem exists
EXCLUDE=$(mktemp)
if [[ $? -ne 0 ]]; then
echo "Could not create a temporary file."
exit 1
fi
curl --fail -o "$EXCLUDE" --silent "https://raw.githubusercontent.com/submit50/submit50/$problem/exclude"
if [[ $? -ne 0 ]]; then
echo "Invalid problem. Did you mean to submit something else?"
exit 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before exiting at line 39, delete $EXCLUDE.

fi

# check for missing files
declare -a files
while read line; do
if [[ "$line" =~ ^# ]]; then
file=$(sed 's/^[ \t]*//;s/[ \t]*$//' <<< "${line#?}")
if [[ ! -e "$file" ]]; then
files+=($file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May as well put $file in double quotes on line 48, in case a file name containing a space ever shows up.

fi
unset file
fi
done < "$EXCLUDE"
unset line
if [[ ${#files[@]} -ne 0 ]]; then
echo "You seem to be missing these files:"
printf " %s\n" "${files[@]}"
read -p "Proceed anyway? " -r read
shopt -s nocasematch
if [[ ! "$read" =~ ^\s*(yes|y)\s*$ ]]; then
echo -e -n "\033[31m"
echo "Submission cancelled."
echo -e -n "\033[39m"
rm -f "$EXCLUDE"
exit 1
fi
shopt -u nocasematch
fi

# ask for GitHub username
while read -p "GitHub username: " -r username
Expand Down Expand Up @@ -73,17 +110,26 @@ if [[ $? -ne 0 ]]; then
if [[ "$username" =~ .*"@".* ]]; then
echo "Log in with your GitHub username, not email address!"
else
echo "Incorrect GitHub username and/or password! Visit https://github.com/password_reset if forgotten."
echo "Incorrect GitHub username and/or password!"
echo "Visit https://github.com/password_reset if forgotten."
fi
rm -f "$EXCLUDE"
exit 1
fi
datetime=$(sed -nr "s/^Date: (.*)/\1/p" <<< "$headers")
# TODO: ensure Date: exists
if [[ -z "$datetime" ]]; then
echo "Can't figure out what time it is!"
echo "Let [email protected] know!"
rm -f "$EXCLUDE"
exit 1
fi

# GET https://api.github.com/user
user=$(curl --config - --fail --silent "https://api.github.com/user" <<< "user = $username:$password" 2>&1)
if [[ $? -ne 0 ]]; then
echo "Sorry, something's wrong! Let [email protected] know!"
echo "Sorry, something's wrong!"
echo "Let [email protected] know!"
rm -f "$EXCLUDE"
exit
fi
email=$(jq '.email // empty' <<< "$user")
Expand All @@ -93,14 +139,16 @@ name=$(jq '.name // empty' <<< "$user")
curl --config - --fail --head --silent "https://api.github.com/repos/submit50/$username" <<< "user = $username:$password" > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Looks like we haven't enabled submit50 for your account yet!"
echo "Email [email protected] and let us know your GitHub username!"
echo "Let [email protected] know your GitHub username!"
rm -f "$EXCLUDE"
exit
fi

# path to the repository
GIT_DIR=$(mktemp -d)
if [[ $? -ne 0 ]]; then
echo "Could not create a temporary directory."
rm -f "$EXCLUDE"
exit 1
fi
export GIT_DIR
Expand All @@ -121,12 +169,11 @@ expect <<- EOF
EOF
if [[ $? -ne 0 ]]; then
echo "Could not clone https://[email protected]/submit50/$username"
rm -rf "$GIT_DIR"
rm -f "$EXCLUDE"
exit 1
fi

# TODO: confirm files to submit
#tree=$(git ls-tree -r "$problem" --name-only | jq --raw-input --slurp @json)

# set options
# https://help.github.com/articles/keeping-your-email-address-private/
git config user.email "${email:[email protected]}"
Expand All @@ -135,12 +182,33 @@ git config user.name "${name:-$username}"
# updates HEAD to point at $problem
git symbolic-ref HEAD "refs/heads/$problem"

# patterns of file names to exclude
git config core.excludesFile "$EXCLUDE"
git config core.ignorecase true

# adds, modifies, and removes index entries to match the working tree
echo -e -n "\033[33m"
echo "git add --all"
echo -e -n "\033[39m"
git add --all

# confirm submission
echo "Files that will be submitted:"
git ls-files | sed -e 's/^/ /'
echo "Files that won't be submitted:"
git ls-files --other | sed -e 's/^/ /'
read -p "Submit? " -r read
shopt -s nocasematch
if [[ ! "$read" =~ ^\s*(yes|y)\s*$ ]]; then
echo -e -n "\033[31m"
echo "Submission cancelled."
echo -e -n "\033[39m"
rm -rf "$GIT_DIR"
rm -f "$EXCLUDE"
exit 1
fi
shopt -u nocasematch

# stores the current contents of the index in a new commit
echo -e -n "\033[33m"
echo "git commit"
Expand Down Expand Up @@ -196,8 +264,9 @@ expect <<- EOF
}
EOF

# remove repository
# remove temporaries
rm -rf "$GIT_DIR"
rm -f "$EXCLUDE"

# kthxbai
echo -e -n "\033[32m"
Expand Down