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.

207 lines
5.4 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 - 2007 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>] [FILES...]
  25. EOT
  26. }
  27. commit_abort() {
  28. echo_error "Got SIGINT (Crtl-C)."
  29. rm -f $tmpfile.status $tmpfile.diff
  30. [ $logfile != $tmpfile.log ] || rm -f $logfile
  31. exit 1
  32. }
  33. shortopts='F:'
  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. tmpfile=$SDEROOT/tmp/$$
  44. mkdir -p "$SDEROOT/tmp"
  45. while [ $# -gt 0 ]; do
  46. case "$1" in
  47. -F|--file) logfile="`readlink -f $2 2> /dev/null`"; shift ;;
  48. --) shift; break ;;
  49. *) echo_abort 1 "Unknown argument '$1', aborting."
  50. esac
  51. shift
  52. done
  53. if [ -n "$logfile" -a ! -r "$logfile" ]; then
  54. echo_abort 2 "File '$logfile' not found."
  55. fi
  56. # validate locations
  57. #
  58. [ $# -gt 0 ] || set -- .
  59. # extract roots at root relative paths of the given locations
  60. locations=$( $SDEROOT/bin/sde-list-files --roots "$@" )
  61. # we only accept one root at the time
  62. roots=$( echo "$locations" | cut -d' ' -f1 | sort -u )
  63. root=$( echo "$roots" | head -n 1 )
  64. [ "$root" == "$roots" ] || echo_abort 1 "Locations from multiple repositories given, aborting."
  65. # $root relative locations
  66. locations=$( echo "$locations" | cut -d' ' -f2 )
  67. [ -n "$locations" ] || echo_abort 1 "No locations, aborting."
  68. # and here we go
  69. #
  70. trap 'commit_abort' INT
  71. # on git we need an empty index
  72. #
  73. [ ! -d "$SDEROOT/$root/.git" ] || ( cd "$SDEROOT/$root/"; git reset > /dev/null )
  74. # find changes on these locations, and apply copyright patch
  75. # - in the case of git working trees we populate the index
  76. #
  77. $SDEROOT/bin/sde-list-changes -C "$SDEROOT/$root" --status $locations | ( while read status file; do
  78. # work sitting on the root of the working tree
  79. cd "$SDEROOT/$root"
  80. if [ -d ".git" ]; then
  81. # git status
  82. case "$status" in
  83. D) # deleted, tell the index
  84. git rm --quiet -- "$file"
  85. ;;
  86. M|?) # modified, update copyright note and add to the index
  87. $SDEROOT/bin/sde-patch-copyright -- "$file"
  88. git add -- "$file"
  89. ;;
  90. *) # unknown
  91. echo_warning "$file: has an unknown status ($status)"
  92. ;;
  93. esac
  94. elif [ -d ".svn" ]; then
  95. # svn status
  96. case "$status" in
  97. ?) # new file, add copyright note and `svn add`
  98. $SDEROOT/bin/sde-patch-copyright -- "$file"
  99. svn add -q -- "$file"
  100. ;;
  101. A|M) # modified or already added, update copyright note
  102. $SDEROOT/bin/sde-patch-copyright -- "$file"
  103. ;;
  104. *) # unknown
  105. echo_warning "$file: has an unknown status ($status)"
  106. ;;
  107. esac
  108. fi
  109. done )
  110. # and finally move to the root of the working tree
  111. cd "$SDEROOT/$root"
  112. # cache stats and diff
  113. if [ -d ".git" ]; then
  114. # git status
  115. git-diff-index -M --color --cached --stat=80,65 HEAD > $tmpfile.status
  116. git-diff-index -M --color --cached -p HEAD | sed -e "s,^\(.\[1m\|\)\(---\|\+\+\+\) [ab]/,\1\2 $root/," > $tmpfile.diff
  117. if [ -z "$logfile" ]; then
  118. logfile=$tmpfile.log
  119. git-diff-index -M --cached -p HEAD | sed -e "s,^\(---\|\+\+\+\) [ab]/,\1 $root/," |
  120. awk -f $SDEROOT/lib/sde-commit/commit-message.awk > $logfile
  121. fi
  122. elif [ -d ".svn" ]; then
  123. # svn status
  124. svn status -- $locations > $tmpfile.status
  125. svn diff -- $locations > $tmpfile.diff
  126. if [ -z "$logfile" ]; then
  127. logfile=$tmpfile.log
  128. cat $tmpfile.diff | sed -e "s,^\(---\|\+\+\+\) [ab]/,\1 $root/," |
  129. awk -f $SDEROOT/lib/sde-commit/commit-message.awk > $logfile
  130. fi
  131. else
  132. echo_abort 3 "$root: unknown version control system."
  133. fi
  134. if [ -s $tmpfile.diff -o -s $tmpfile.status ]; then
  135. echo_msg "Changes:"
  136. cat $tmpfile.diff
  137. echo_msg "Status:"
  138. cat $tmpfile.status
  139. quit=0
  140. until [ $quit -ne 0 ]; do
  141. echo_msg "Commit Message:"
  142. if [ -s $logfile ]; then
  143. cat $logfile
  144. commit=",${COLOR_MESSAGE}c${COLOR_NORMAL}:commit"
  145. else
  146. echo_warning "\tEmpty Log File!"
  147. commit=
  148. fi
  149. echo -en "\n${COLOR_MESSAGE}Is the message ok?${COLOR_NORMAL}"
  150. echo -en " (${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} "
  151. read opt
  152. case "$opt" in
  153. c*) if [ -z "$commit" ]; then
  154. # don't commit yet
  155. continue
  156. elif [ -d ".git" ]; then
  157. # git commit
  158. git commit --file $logfile && rm "$logfile" ; quit=1
  159. elif [ -d ".svn" ]; then
  160. # svn commit
  161. svn commit --file $logfile -- $locations && rm "$logfile" ; quit=1
  162. fi ;;
  163. d*) ${PAGER:-less -R} $tmpfile.diff ;;
  164. s*) cat $tmpfile.status ;;
  165. e*) ${EDITOR:-vi} $logfile ;;
  166. q*) quit=1 ;;
  167. *) echo "Excuse me?"
  168. esac
  169. done
  170. else
  171. echo_warning "No changes detected."
  172. fi
  173. rm -f $tmpfile.status $tmpfile.diff
  174. [ "$logfile" != "$tmpfile.log" ] || rm -f "$logfile"