Skip to content

Instantly share code, notes, and snippets.

@sheep0x
Created March 1, 2015 04:31
Show Gist options
  • Save sheep0x/c7d7f631ffff4837e435 to your computer and use it in GitHub Desktop.
Save sheep0x/c7d7f631ffff4837e435 to your computer and use it in GitHub Desktop.
Bash/Zsh customization for UCB CS 61B Spring 2015
#
# Copyright 2015 Chen Ruichao <linuxer.sheep.0x@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# should support Bash 4 and Zsh 5
# TODO
# -f to skip assignment name check
# warning if assignment is due or ag not started
# if SHEEP0X_CS61B_AUTO_UPDATE is true, auto pull newer version of this script
# special branch: survey
# UX:
# can we auto add & provide an interactive commit message guide? (maybe use git's interactive add?)
# filter output of git push to remove messages that are not too useful (compression, etc.)
# enter password only once (better use ssh-agent to do this)
# sanity check:
# origin is correct, skeleton is correct, ...
# extra check for conservative people: HEAD on master, HEAD on branch tip, master is the only local branch, ...
# check magic_word.txt
# Use associative array for possible better performance?
# (for loop vs key lookup. Bash probably has a extremely slow for loop)
# documentation:
# interface:
# SHEEP0X_CS61B_USE_DEFAULT_SETUP
# __sheep0x_cs61b_autograde
# __sheep0x_cs61b_submit
# failure behaviors:
# git repo not found => use Git's error msg and retval
# git not installed or not found in PATH
#
# $# is not checked. There must be exactly one arg.
# $1 - assignment name
# retval - whether $1 is a valid assignment name
# Print message to stderr if assignment is invalid.
#
function __sheep0x_cs61b_check_assignment() {
local -a valid invalid
valid=(
lab{1..3} lab{6..10} lab{12..13}
hw{1..9}
proj{0..3}
)
invalid=(
lab1{b..d} lab{4,5,11,14}
hw0
)
local ass="$1"
local n
for n in "${valid[@]}"; do
[[ $ass == $n ]] && return
done
for n in "${invalid[@]}"; do
[[ $ass == $n ]] && {
printf '%s does not need to be submitted\n' "$ass" >&2
return 1
}
done
printf 'Unknown assignment: %s\n' "$ass" >&2
return 1
}
#
# Push current commit (HEAD) to autograder or submit branch
# $1 - 'ag' or 'submit', what kind of branch to push to
# $2 - assignment name
# (similar to: git push origin $1/$2)
#
# Assume origin is the repo to push to
# No sanity check is performed on $# and $1
# If assignment name didn't pass sanity check, complain and return
# false, nothing will happen.
#
function __sheep0x_cs61b_hand_in {
# check that assignment is valid
local what="$1" ass="$2"
__sheep0x_cs61b_check_assignment "$ass" || return 1
git push origin HEAD:"$what/$ass"
}
# Number of arguments is not checked
# Assignment name is checked in __sheep0x_cs61b_hand_in
# See __sheep0x_cs61b_hand_in
function __sheep0x_cs61b_autograde { __sheep0x_cs61b_hand_in 'ag' "$1"; }
function __sheep0x_cs61b_submit { __sheep0x_cs61b_hand_in 'submit' "$1"; }
# We don't want to create name pollution.
if [[ $SHEEP0X_CS61B_USE_DEFAULT_SETUP == yes ]]; then
function submit {
# check number of args
if ! [[ $# == 1 ]]; then
printf 'Usage: submit ASSIGNMENT\n' >&2
return 1
fi
__sheep0x_cs61b_submit "$@"
}
function ag {
# check number of args
if ! [[ $# == 1 ]]; then
printf 'Usage: ag ASSIGNMENT\n' >&2
return 1
fi
__sheep0x_cs61b_autograde "$@"
}
fi
# vim: ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment