OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
5.8 KiB

  1. #!/bin/sh
  2. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. #
  5. # Filename: bin/sde-commit
  6. # Copyright (C) 2006 - 2009 The OpenSDE Project
  7. # Copyright (C) 2004 - 2006 The T2 SDE Project
  8. #
  9. # More information can be found in the files COPYING and README.
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; version 2 of the License. A copy of the
  14. # GNU General Public License can be found in the file COPYING.
  15. # --- SDE-COPYRIGHT-NOTE-END ---
  16. #Description: Commits into svn the changes on the given locations
  17. #Alias: ci
  18. [ -n "$SDEROOT" ] ||
  19. export SDEROOT=$( cd "${0%/*}/.."; pwd -P )
  20. . $SDEROOT/lib/libsde.in
  21. commit_usage() {
  22. local progname=${0##*/}
  23. cat <<EOT
  24. Usage: $progname [--file <message file>] [-m <message>] [FILES...]
  25. EOT
  26. }
  27. commit_abort() {
  28. echo_error "Got SIGINT (Crtl-C)."
  29. rm -f $tmpfile.status $tmpfile.diff
  30. [ -n "$logfile" -a "$logfile" != "$tmpfile.log" ] || rm -f "$logfile"
  31. exit 1
  32. }
  33. shortopts='F:m:s'
  34. longopts='file:'
  35. options=$( getopt -o "$shortopts" -l "$longopts" -- "$@" )
  36. if [ $? -ne 0 ]; then
  37. commit_usage
  38. exit -1
  39. fi
  40. # load new arguments list
  41. eval set -- "$options"
  42. logfile=
  43. message=
  44. signed=
  45. tmpfile=$SDEROOT/tmp/$$
  46. mkdir -p "$SDEROOT/tmp"
  47. while [ $# -gt 0 ]; do
  48. case "$1" in
  49. -F|--file) logfile="`readlink -f $2 2> /dev/null`"; shift ;;
  50. -m) message="$2"; shift ;;
  51. -s) signed=true; shift ;;
  52. --) shift; break ;;
  53. -*) echo_abort 1 "Unknown argument '$1', aborting." ;;
  54. *) break ;;
  55. esac
  56. shift
  57. done
  58. if [ -n "$logfile" -a ! -r "$logfile" ]; then
  59. echo_abort 2 "File '$logfile' not found."
  60. fi
  61. # validate locations
  62. #
  63. [ $# -gt 0 ] || set -- .
  64. # extract roots at root relative paths of the given locations
  65. locations=$( $SDEROOT/bin/sde-list-files --roots "$@" )
  66. # we only accept one root at the time
  67. roots=$( echo "$locations" | cut -d' ' -f1 | sort -u )
  68. root=$( echo "$roots" | head -n 1 )
  69. [ "$root" = "$roots" ] || echo_abort 1 "Locations from multiple repositories given, aborting."
  70. # $root relative locations
  71. locations=$( echo "$locations" | cut -d' ' -f2 )
  72. [ -n "$locations" ] || echo_abort 1 "No locations, aborting."
  73. # and here we go
  74. #
  75. trap 'commit_abort' INT
  76. # on git we need an empty index
  77. #
  78. [ ! -d "$SDEROOT/$root/.git" ] || ( cd "$SDEROOT/$root/"; git reset > /dev/null )
  79. # find changes on these locations, and apply copyright patch
  80. # - in the case of git working trees we populate the index
  81. #
  82. $SDEROOT/bin/sde-list-changes -C "$SDEROOT/$root" --status $locations | ( while read status file; do
  83. # work sitting on the root of the working tree
  84. cd "$SDEROOT/$root"
  85. if [ -d ".git" ]; then
  86. # git status
  87. case "$status" in
  88. D) # deleted, tell the index
  89. git rm --quiet -- "$file"
  90. ;;
  91. M|?) # modified, update copyright note and add to the index
  92. $SDEROOT/bin/sde-patch-copyright -- "$file"
  93. git add -- "$file"
  94. ;;
  95. *) # unknown
  96. echo_warning "$file: has an unknown status ($status)"
  97. ;;
  98. esac
  99. elif [ -d ".svn" ]; then
  100. # svn status
  101. case "$status" in
  102. ?) # new file, add copyright note and `svn add`
  103. $SDEROOT/bin/sde-patch-copyright -- "$file"
  104. svn add -q -- "$file"
  105. ;;
  106. A|M) # modified or already added, update copyright note
  107. $SDEROOT/bin/sde-patch-copyright -- "$file"
  108. ;;
  109. *) # unknown
  110. echo_warning "$file: has an unknown status ($status)"
  111. ;;
  112. esac
  113. fi
  114. done )
  115. # and finally move to the root of the working tree
  116. cd "$SDEROOT/$root"
  117. # cache stats and diff
  118. if [ -d ".git" ]; then
  119. # git status
  120. git diff-index -M --color --cached --stat=80,65 HEAD > $tmpfile.status
  121. git diff-index -M --color --cached -p HEAD | sed -e "s,^\(.\[1m\|\)\(---\|\+\+\+\) [ab]/,\1\2 $root/," > $tmpfile.diff
  122. if [ -z "$logfile" -a -z "$message" ]; then
  123. logfile=$tmpfile.log
  124. git diff-index -M --cached -p HEAD | sed -e "s,^\(---\|\+\+\+\) [ab]/,\1 $root/," |
  125. gawk -f $SDEROOT/lib/sde-commit/commit-message.awk > $logfile
  126. fi
  127. elif [ -d ".svn" ]; then
  128. # svn status
  129. svn status -- $locations > $tmpfile.status
  130. svn diff -- $locations > $tmpfile.diff
  131. if [ -z "$logfile" -a -z "$message" ]; then
  132. logfile=$tmpfile.log
  133. cat $tmpfile.diff | sed -e "s,^\(---\|\+\+\+\) [ab]/,\1 $root/," |
  134. gawk -f $SDEROOT/lib/sde-commit/commit-message.awk > $logfile
  135. fi
  136. else
  137. echo_abort 3 "$root: unknown version control system."
  138. fi
  139. if [ -n "$message" ]; then
  140. logfile=$tmpfile.log
  141. echo "$message" > $logfile
  142. fi
  143. if [ -s $tmpfile.diff -o -s $tmpfile.status ]; then
  144. echo_msg "Changes:"
  145. cat $tmpfile.diff
  146. echo_msg "Status:"
  147. cat $tmpfile.status
  148. if [ -s "$logfile" -a -n "${signed:-}" -a -e '.git' ]; then
  149. cat <<-EOT >> $logfile
  150. Signed-off-by: $(git config user.name) <$(git config user.email)>
  151. EOT
  152. fi
  153. quit=0
  154. until [ $quit -ne 0 ]; do
  155. echo_msg "Commit Message:"
  156. if [ -s $logfile ]; then
  157. cat $logfile
  158. commit=",${COLOR_MESSAGE}c${COLOR_NORMAL}:commit"
  159. else
  160. echo_warning "\tEmpty Log File!"
  161. commit=
  162. fi
  163. $ECHO_E -n "\n${COLOR_MESSAGE}Is the message ok?${COLOR_NORMAL}"
  164. $ECHO_E -n " (${COLOR_MESSAGE}q${COLOR_NORMAL}:quit,${COLOR_MESSAGE}e${COLOR_NORMAL}:edit,${COLOR_MESSAGE}d${COLOR_NORMAL}:diff,${COLOR_MESSAGE}s${COLOR_NORMAL}:status$commit)${COLOR_MESSAGE}:${COLOR_NORMAL} "
  165. read opt
  166. case "$opt" in
  167. c*) if [ -z "$commit" ]; then
  168. # don't commit yet
  169. continue
  170. elif [ -d ".git" ]; then
  171. # git commit
  172. git commit ${signed:+-s} --file $logfile && rm "$logfile" ; quit=1
  173. elif [ -d ".svn" ]; then
  174. # svn commit
  175. svn commit --file $logfile -- $locations && rm "$logfile" ; quit=1
  176. fi ;;
  177. d*) ${PAGER:-less -R} $tmpfile.diff ;;
  178. s*) cat $tmpfile.status ;;
  179. e*) ${EDITOR:-vi} $logfile ;;
  180. q*) quit=1 ;;
  181. *) echo "Excuse me?"
  182. esac
  183. done
  184. else
  185. echo_warning "No changes detected."
  186. fi
  187. rm -f $tmpfile.status $tmpfile.diff
  188. [ "$logfile" != "$tmpfile.log" ] || rm -f "$logfile"