diff --git a/tools/tools/git/git-arc.sh b/tools/tools/git/git-arc.sh --- a/tools/tools/git/git-arc.sh +++ b/tools/tools/git/git-arc.sh @@ -43,6 +43,14 @@ exit 1 } +cleanup() +{ + rc=$? + rm -fr "$GITARC_TMPDIR" + trap - EXIT + exit $rc +} + err_usage() { cat >&2 <<__EOF__ @@ -236,12 +244,11 @@ err "invalid diff ID $diff" fi - tmp=$(mktemp) + tmp="${GITARC_TMPDIR}/tmp" echo '{"names":["'"$diff"'"]}' | arc_call_conduit -- phid.lookup > "$tmp" phid=$(jq -r ".response[].phid" "$tmp") if [ -z "$phid" ] || [ "$phid" = "null" ]; then - rm -f "$tmp" err "invalid Phabricator ID for ${diff}" fi @@ -255,7 +262,6 @@ summary=$(jq -r "select(.response != []) | .response.${diff}.fullName" < "$tmp") printf "${term_color}%-15s\033[0m %s\n" "${status_name}" "${summary}" - rm -f "$tmp" } log2diff() @@ -328,7 +334,7 @@ return 1 fi - msg=$(mktemp) + msg="${GITARC_TMPDIR}/msg" git show -s --format='%B' "$commit" > "$msg" printf "\nTest Plan:\n" >> "$msg" printf "\nReviewers:\n" >> "$msg" @@ -357,7 +363,6 @@ ]}' | arc_call_conduit -- differential.revision.edit >&3 fi - rm -f "$msg" return 0 } @@ -377,7 +382,7 @@ jq '.response.data[0].attachments.reviewers.reviewers[] | select(.status == "accepted").reviewerPHID') if [ -n "$userids" ]; then echo '{ - "constraints": {"phids": ['"$(printf "%s" "$userids" | tr '[:space:]' ',')"']} + "constraints": {"phids": ['"$(echo "$userids" | tr '[:blank:]' ',')"']} }' | arc_call_conduit -- user.search | jq -r '.response.data[].fields.username' @@ -591,31 +596,30 @@ patch_commit() { - local diff reviewid review_data authorid user_data user_addr user_name author - local tmp author_addr author_name + local diff reviewid review_data authorid user_data user_addr user_name + local diff_data author_addr author_name author tmp diff=$1 reviewid=$(diff2phid "$diff") # Get the author phid for this patch - review_data=$(mktemp) + review_data="${GITARC_TMPDIR}/review" echo '{"constraints": {"phids": ["'"$reviewid"'"]}}' | \ arc_call_conduit -- differential.revision.search > "$review_data" authorid=$(jq -r '.response.data[].fields.authorPHID' "$review_data") # Get metadata about the user that submitted this patch - user_data=$(mktemp) + user_data="${GITARC_TMPDIR}/user" echo '{"constraints": {"phids": ["'"$authorid"'"]}}' | \ arc_call_conduit -- user.search | \ jq -r '.response.data[].fields' > "$user_data" user_addr=$(jq -r '.username' "$user_data") user_name=$(jq -r '.realName' "$user_data") - rm "$user_data" # Dig the data out of querydiffs api endpoint, although it's deprecated, # since it's one of the few places we can get email addresses. It's unclear # if we can expect multiple difference ones of these. Some records don't # have this data, so we remove all the 'null's. We sort the results and # remove duplicates 'just to be sure' since we've not seen multiple # records that match. - diff_data=$(mktemp) + diff_data="${GITARC_TMPDIR}/diff" echo '{"revisionIDs": [ '"${diff#D}"' ]}' | \ arc_call_conduit -- differential.querydiffs | jq -r '.response | flatten | .[]' > "$diff_data" @@ -632,7 +636,6 @@ fi author=$(find_author "$user_addr" "$user_name" "$author_addr" "$author_name") - rm "$diff_data" # If we had to guess, and the user didn't want to guess, abort if [ "${author}" = "ABORT" ]; then @@ -640,12 +643,11 @@ exit 1 fi - tmp=$(mktemp) - jq -r '.response.data[].fields.title' "$review_data" > $tmp - echo >> $tmp - jq -r '.response.data[].fields.summary' "$review_data" >> $tmp - echo >> $tmp - rm "$review_data" + tmp="${GITARC_TMPDIR}/tmp" + jq -r '.response.data[].fields.title' "$review_data" > "$tmp" + echo >> "$tmp" + jq -r '.response.data[].fields.summary' "$review_data" >> "$tmp" + echo >> "$tmp" # XXX this leaves an extra newline in some cases. reviewers=$(diff2reviewers "$diff" | sed '/^$/d' | paste -sd ',' - | sed 's/,/, /g') if [ -n "$reviewers" ]; then @@ -654,7 +656,6 @@ # XXX TODO refactor with gitarc__stage maybe? printf "Differential Revision:\thttps://reviews.freebsd.org/%s\n" "${diff}" >> "$tmp" git commit --author "${author}" --file "$tmp" - rm "$tmp" } gitarc__patch() @@ -714,7 +715,7 @@ git checkout -q -b "${branch}" main fi - tmp=$(mktemp) + tmp="${GITARC_TMPDIR}/tmp" for commit in $commits; do git show -s --format=%B "$commit" > "$tmp" title=$(git show -s --format=%s "$commit") @@ -875,4 +876,7 @@ BROWSE=--browse fi +GITARC_TMPDIR=$(mktemp -d) +trap cleanup EXIT HUP INT QUIT TRAP USR1 TERM + gitarc__"${verb}" "$@"