File: cygwin-wrapper.bash

package info (click to toggle)
darcs 2.18.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,960 kB
  • sloc: haskell: 47,748; sh: 13,466; ansic: 447; perl: 134; makefile: 8
file content (264 lines) | stat: -rw-r--r-- 8,271 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#! /bin/bash

DIRNAME=`dirname "${0}"`
if [ "${DIRNAME:0:1}" = "/" ] ; then
 DARCSPACKAGEDIR="${DIRNAME}"
else
 DARCSPACKAGEDIR="${PWD}/${DIRNAME}"
fi

# If the DARCSPACKAGEDIR assignment above doesn't work for some funny reason, 
# you could set these variables by hand.  Or fix the script to work 
# automatically and submit a patch.

# Should be set to the full Cygwin path to the directory containing the 
# putty executables.
putty_binary_dir="${DARCSPACKAGEDIR}"
# Should be set to the full Cygwin path to the directory containing the 
# Windows binary "darcs.exe".
darcs_binary_dir="$putty_binary_dir"
# Should be set to the full Cygwin path to the Windows binary
# "darcs.exe".
darcs_binary="${darcs_binary_dir}/realdarcs.exe"

#---------------------------------------------------------------------
# Darcs Wrapper for Cygwin
#
# A Bash script that allows Cywin paths on the command line when using
# a version of Darcs compiled for Windows.  Darcs will still use still
# Windows paths internally.
#
#---------------------------------------------------------------------
# Usage
# 
# Edit this file and set the variables above.  Then, rename this 
# script to "darcs" and put it in your PATH somewhere before the 
# original binary.
#
# Darcs needs to launch itself for some operations and so the original
# binary needs to be in your Windows PATH.  Do not rename it.
#
#---------------------------------------------------------------------
# Known Issues
#
# This script is just a stopgap measure.  Things don't work perfectly.
# We really need a Cygwin build of Darcs.
#
# No path conversion is performed on:
#    - Any preferences set with "setpref"
#    - The "COMMAND" argument to "darcs trackdown"
#
# When Darcs launches external programs, it uses a Windows system call
# to do so.  This means you may not be able to run "hash bang" scripts
# directly.  For example, to run the Bash script "myscript", you'll
# have to tell Darcs to run "bash myscript".
#
# --------------------------------------------------------------------


PATH="$putty_binary_dir:$darcs_binary_dir:${PATH}"

debug=false

cmd="$1"

# Print each argument to stderr on a separate line.  Then exit.
function die() {
   local line
   for line in "$@"; do
      echo "$line" > /dev/stderr
   done
   exit 2
}

# Make sure 'darcs_binary_dir' is set.
if [ ! -d "$darcs_binary_dir" ]; then
   die "Please edit this script and set the 'darcs_binary_dir' variable" \
       "to refer to a valid directory." \
       "      script path = '$0'"  \
       "     darcs_binary_dir = '$darcs_binary_dir'"
fi

# Special case for when the first argument is an option.
if expr match "$cmd" '-' > /dev/null; then
   if $debug; then
      # echo "SIMPLE CASE:"
      for arg in "$@"; do
         echo "  arg = '$arg'"
      done
   else
      # echo about to exec -a darcs "$darcs_binary" "$@"
      exec -a darcs "$darcs_binary" "$@"
   fi
fi

# Shift off the darcs command name
shift

function is_opaque_opt() {
   local opt
   for opt in "${opaque_binary_opts[@]}"; do
      if [ "$opt" == "$1" ]; then
         return 0
      fi
   done
   return 1
}

function is_file_opt() {
   local opt
   for opt in "${file_binary_opts[@]}"; do
      if [ "$opt" == "$1" ]; then
         return 0
      fi
   done
   return 1
}

# Options are not dealt with in a command-specific way.  AFAIK, Darcs
# doesn't use the same option in two different ways, so we should be
# fine.

# List of "opaque" binary options.  These are options where we don't
# treat the option argument like a file.
declare -a opaque_binary_opts=( \
   '--repo-name' \
   '--to-match' '--to-patch' '--to-tag' '--to-hash' \
   '--from-match' '--from-patch' '--from-tag' '--from-hash' \
   '-t' '--tag' '--tags' '--tag-name' \
   '-p' '--patch' '--patches' \
   '-m' '--patch-name' \
   '-h' '--hash' \
   '--matches' '--match' \
   '--token-chars' \
   '-A' '--author' '--from' '--to' '--cc' \
   '--sign-as' '--creator-hash' \
   '--last' '--diff-opts' \
   '-d' '--dist-name' \
   '--log-file' \
   '--apply-as' \
   )

# List of binary options that take file arguments that need to be converted.
declare -a file_binary_opts=( \
   '--repodir' '--repo' '--sibling' \
   '--context' \
   '--logfile' '-o' '--output' \
   '--external-merge' \
   '--sign-ssl' '--verify' '--verify-ssl' \
   )

# --------------------------------------------------------------------
# The three command categories.  We only use the first one, but the
# others are listed to make sure we've covered everything.  Luckily,
# there aren't any commands that have some args that need to be
# converted and some that don't.

# Commands whose arguments are file paths that need to be translated.
cmds_convert_nonoption_args='|clone|pull|push|send|apply'

# Commands who's arguments should be left alone.  File paths that
# refer to files in the repo should NOT be converted because they
# are relative paths, which Darcs will handle just fine.  Cygwin
# sometimes makes them absolute paths, which confuses Darcs.
#cmds_no_convert_nonoption_paths='|add|remove|mv|replace|record|whatsnew|log|setpref|test|amend|revert|diff|annotate'

# Commands that don't accept non-option arguments
#cmds_no_nonoption_args='|initialize|tag|optimize|rollback|unrecord|unpull|dist|repair'

# See if we need to convert the non-option args for the current
# command.  This matches some prefix of one of the commands in the
# list.  The match may not be unambiguous, we can rely on Darcs to
# deal with that correctly.
if expr match "$cmds_convert_nonoption_args" ".*|$cmd" > /dev/null; then
   convert_nonoption_args=true
else
   convert_nonoption_args=false
fi

function convert_path() {
   # echo "converting path ${*} ..." >> /tmp/log
   if expr match "$1" '[-@._A-Za-z0-9]*:' > /dev/null; then
      # Some sort of URL or remote ssh pathname ("xxx:/")
      echo "$1"
      # echo "converting path ${*} ... to ${1}" >> /tmp/log
   elif [ "$1" == '.' ]; then
      # Compensate for stupid 'cygpath' behavior.
      echo '.'
      # echo "converting path ${*} ... to ." >> /tmp/log
   else
      cygpath -wl -- "$1"
      # echo "converting path ${*} ... to `cygpath -wl -- ${1}`" >> /tmp/log
   fi
}

declare -a params=("$cmd")

num_nonoption_args=0

while [ $# -gt 0 ]; do
   arg=$1
   shift
   if expr match "$arg" '-' > /dev/null; then
      # It's an option.  Check to see if it's an opaque binary option.

      if expr match "$arg" '.*=' > /dev/null; then
         # The option has an '=' in it.
         opt=`expr match "$arg" '\([^=]*\)'`
         opt_arg=`expr match "$arg" '[^=]*=\(.*\)'`
         if is_opaque_opt "$opt"; then
            true;
         elif is_file_opt "$opt"; then
            opt_arg=`convert_path "$opt_arg"`
         else
            die "darcs-wrapper: I don't think '$opt' accepts an argument." \
                "[ If it does, then there is a bug in the wrapper script. ]"
         fi
         params[${#params[*]}]="$opt=$opt_arg"

      else
         # The option doesn't have an '='
         opt="$arg"
         if is_opaque_opt "$opt"; then
            if [ $# -eq 0 ]; then
               die "darcs-wrapper: I think '$arg' requires an argument." \
                   "[ If it doesn't, then there is a bug in the wrapper script. ]"
            fi
            opt_arg="$1"
            shift
            params[${#params[*]}]="$opt"
            params[${#params[*]}]="$opt_arg"
         elif is_file_opt "$opt"; then
            if [ $# -eq 0 ]; then
               die "darcs-wrapper: I think '$arg' requires an argument." \
                   "[ If it doesn't, then there is a bug in the wrapper script. ]"
            fi
            opt_arg=`convert_path "$1"`
            shift
            params[${#params[*]}]="$opt"
            params[${#params[*]}]="$opt_arg"
         else
            params[${#params[*]}]="$opt"
         fi
      fi

   else
      if $convert_nonoption_args; then
         arg=`convert_path "$arg"`
      fi
      params[${#params[*]}]="$arg"
      (( num_nonoption_args += 1 ))
   fi
done

# DEBUG
if $debug; then
   echo "ARGS:"
   for arg in "${params[@]}"; do
      echo "  arg = '$arg'"
   done
else
   # echo about to exec -a darcs "$darcs_binary" "${params[@]}"
   exec -a darcs "$darcs_binary" "${params[@]}"
fi