forked from git-ftp/git-ftp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-ftp
More file actions
executable file
·922 lines (823 loc) · 23.6 KB
/
Copy pathgit-ftp
File metadata and controls
executable file
·922 lines (823 loc) · 23.6 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
#!/usr/bin/env bash
#
# Copyright(c) 2010 René Moser
# http://github.com/resmo/git-ftp
#
# Git-ftp is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Git-ftp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Git-ftp. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------
# Setup Environment
# ------------------------------------------------------------
readonly STARTDIR=$(pwd)
cd $(git rev-parse --show-toplevel) || print_error_and_die "Not a Git project? Exiting..." 8
# General config
readonly DEFAULT_PROTOCOL="ftp"
readonly DEPLOYED_SHA1_FILE=".git-ftp.log"
readonly LCK_FILE=".git/$(basename $0).lck"
readonly REMOTE_LCK_FILE="$(basename $0).lck"
readonly SYSTEM=$(uname)
readonly VERSION='0.5.2'
# ------------------------------------------------------------
# Defaults
# ------------------------------------------------------------
URL=""
REMOTE_PROTOCOL=""
REMOTE_CACERT=""
REMOTE_HOST=""
REMOTE_USER=""
REMOTE_PASSWD=""
REMOTE_PATH=""
REMOTE_DELETE_CMD="-DELE "
REMOTE_CMD_OPTIONS="-s"
ACTION=""
LOG_CACHE=""
SCOPE=""
KEYCHAIN_USER=""
KEYCHAIN_HOST=""
DEPLOYED_SHA1=""
declare -a CURL_ARGS
declare -i CURL_INSECURE=0
declare -i VERBOSE=0
declare -i IGNORE_DEPLOYED=0
declare -i DRY_RUN=0
declare -i FORCE=0
declare -i ENABLE_REMOTE_LCK=0
declare -i ACTIVE_MODE=0
declare -i USE_KEYCHAIN=0
declare -i UNSTAGED=0
declare -i UNCOMMITED=0
# ------------------------------------------------------------
# Constant Exit Error Codes
# ------------------------------------------------------------
readonly ERROR_USAGE=2
readonly ERROR_MISSING_ARGUMENTS=3
readonly ERROR_UPLOAD=4
readonly ERROR_DOWNLOAD=5
readonly ERROR_UNKNOWN_PROTOCOL=6
readonly ERROR_REMOTE_LOCKED=7
readonly ERROR_GIT=8
# ------------------------------------------------------------
# Functions
# ------------------------------------------------------------
usage_long()
{
local pager=$(git config --get core.pager)
${GIT_PAGER:-${pager:-${PAGER:-less}}} << EOF
USAGE
git-ftp <action> [<options>] <url>
DESCRIPTION
git-ftp does FTP the GIT way.
It uses GIT to find out which files are different of the uploaded files
and let you save time and bandwith by uploading only those files.
It remembers the deployed state by uploading the SHA1 in a log file.
ACTIONS
. init
Initialize the first upload to remote host.
. push
Uploads git tracked files which have changed since last upload.
. show
Gets last uploaded SHA1 from log and hooks git show.
. catchup
Uploads the current SHA1 to the log, does not upload any files.
This is useful if you used another FTP client to upload the
files and now want to remember the SHA1.
. help
Shows this help
URL
. FTP (default) host.example.com[:<port>][/<remote path>]
. FTP ftp://host.example.com[:<port>][/<remote path>]
. SFTP sftp://host.example.com[:<port>][/<remote path>]
. FTPS ftps://host.example.com[:<port>][/<remote path>]
. FTPES ftpes://host.example.com[:<port>][/<remote path>]
OPTIONS
-h, --help Show this help
-u, --user FTP login name
-p, --passwd FTP password
-k, --keychain FTP password from KeyChain (Mac OS X only)
-s, --scope Using a scope (e.g. dev, production, testing, foobar)
-D, --dry-run Dry run: Does not upload anything
-a, --all Uploads all files, ignores deployed SHA1 hash
-c, --commit Sets SHA1 hash of last deployed commit by option
-A, --active Use FTP active mode
-l, --lock Enable/Disable remote locking
-f, --force Force, does not ask questions
-n, --silent Silent
-v, --verbose Verbose
--uncommited Push uncommited files, both staged and not
--unstaged Push unstaged files, good for testing
--cacert <file> Use <file> as certificate for server authentication
--version Prints version
EXAMPLES
. git-ftp push -u john ftp://ftp.example.com:4445/public_ftp -p -v
. git-ftp push -p -u john -v ftp.example.com:4445:/public_ftp
. git-ftp push --scope production
SET DEFAULTS
. git config git-ftp.user john
. git config git-ftp.url ftp.example.com
. git config git-ftp.password secr3t
SET SCOPE DEFAULTS
e.g. your scope is 'testing'
. git config git-ftp.testing.url ftp.example.local
VERSION
$VERSION
EOF
exit 0
}
usage() {
echo "git-ftp <action> [<options>] <url>"
exit $ERROR_USAGE
}
escape() {
echo "$1" | sed 's/\([\.\+\$]\)/\\\1/g'
}
has() {
local item=$1; shift
echo " $@ " | grep -q " $(escape $item) "
}
git_submodules() {
git submodule | awk '{print $2}'
}
is_submodule() {
has $1 $(git_submodules)
}
ask_for_passwd() {
echo -n "Password: "
stty -echo > /dev/null 2>&1
read REMOTE_PASSWD
stty echo > /dev/null 2>&1
echo ""
}
get_keychain_password () {
if [ "$SYSTEM" = "Darwin" ]; then
# Split user and host if necessary
if [ $(echo "$KEYCHAIN_USER" | grep '@') ]; then
KEYCHAIN_HOST=$(echo "$KEYCHAIN_USER" | cut -d '@' -f2)
KEYCHAIN_USER=$(echo "$KEYCHAIN_USER" | cut -d '@' -f1)
else
[ -z $KEYCHAIN_USER ] && KEYCHAIN_USER=$REMOTE_USER
[ -z $KEYCHAIN_HOST ] && KEYCHAIN_HOST=$REMOTE_HOST
fi
[ -z $KEYCHAIN_USER ] && print_error_and_die "Missing keychain account" $ERROR_MISSING_ARGUMENTS
CURL_ARGS=(-a "$KEYCHAIN_USER")
[ -n "$KEYCHAIN_HOST" ] && CURL_ARGS+=(-s "$KEYCHAIN_HOST")
local pass
if pass="$(security find-internet-password ${CURL_ARGS[@]} -g 2>&1 > /dev/null)"; then
without_prefix="${pass#password: \"}"
REMOTE_PASSWD="${without_prefix%\"}"
else
print_error_and_die "Password not found in keychain for account '$KEYCHAIN_USER @ $KEYCHAIN_HOST'." $ERROR_MISSING_ARGUMENTS
fi
else
write_log "Ingoring -k on non-Darwin systems"
fi
}
# Checks if last comand was successful
check_exit_status() {
if [ $? -ne 0 ]; then
print_error_and_die "$1, exiting..." $2
fi
}
# Simple log func
write_log() {
if [ $VERBOSE -eq 1 ]; then
echo "$(date): $1"
else
if [ -n "$LOG_CACHE" ]; then
LOG_CACHE="$LOG_CACHE\n$(date): $1"
else
LOG_CACHE="$(date): $1"
fi
fi
}
# Simple error printer
print_error_and_die() {
if [ $VERBOSE -eq 0 ]; then
echo "fatal: $1"
else
write_log "fatal: $1"
fi
release_lock
exit $2
}
# Simple info printer
print_info() {
if [ $VERBOSE -eq 0 ]; then
echo "$1"
else
write_log "$1"
fi
}
set_default_curl_options() {
OIFS="$IFS"
IFS=" "
CURL_ARGS=(${REMOTE_CMD_OPTIONS[@]})
IFS="$OIFS"
CURL_ARGS+=(--retry 5)
CURL_ARGS+=(--globoff)
if [ ! -z $REMOTE_USER ]; then
CURL_ARGS+=(--user "$REMOTE_USER":"$REMOTE_PASSWD")
else
CURL_ARGS+=(--netrc)
fi
CURL_ARGS+=(-#)
if [ $ACTIVE_MODE -eq 1 ]; then
CURL_ARGS+=(-P "-")
fi
}
upload_file() {
local SRC_FILE="$1"
local DEST_FILE="$2"
if [ -z $DEST_FILE ]; then
DEST_FILE=$SRC_FILE
fi
set_default_curl_options
CURL_ARGS+=(-T "$SRC_FILE")
CURL_ARGS+=(--ftp-create-dirs)
CURL_ARGS+=("$REMOTE_PROTOCOL://$REMOTE_HOST/${REMOTE_PATH}${DEST_FILE}")
curl "${CURL_ARGS[@]}"
check_exit_status "Could not upload" $ERROR_UPLOAD
}
remove_file() {
local FILENAME="$1"
set_default_curl_options
CURL_ARGS+=(-Q "${REMOTE_DELETE_CMD}${REMOTE_PATH}${FILENAME}")
CURL_ARGS+=("$REMOTE_PROTOCOL://$REMOTE_HOST")
if [ "$REMOTE_CMD_OPTIONS" = "-v" ]; then
curl "${CURL_ARGS[@]}"
else
curl "${CURL_ARGS[@]}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
write_log "Could not delete ${REMOTE_PATH}${FILENAME}, continuing..."
fi
fi
}
remove_dir() {
ORIGIN_REMOTE_DELETE_CMD=$REMOTE_DELETE_CMD
REMOTE_DELETE_CMD="RMD "
remove_file $1
REMOTE_DELETE_CMD=$ORIGIN_REMOTE_DELETE_CMD
}
get_file_content() {
local SRC_FILE="$1"
set_default_curl_options
CURL_ARGS+=("$REMOTE_PROTOCOL://$REMOTE_HOST/${REMOTE_PATH}${SRC_FILE}")
curl "${CURL_ARGS[@]}"
}
upload_local_sha1() {
DEPLOYED_SHA1=$(git log -n 1 --pretty=format:%H)
write_log "Uploading commit log to $REMOTE_PROTOCOL://$REMOTE_HOST/${REMOTE_PATH}$DEPLOYED_SHA1_FILE"
if [ $DRY_RUN -ne 1 ]; then
echo "$DEPLOYED_SHA1" | upload_file - $DEPLOYED_SHA1_FILE
check_exit_status "Could not upload" $ERROR_UPLOAD
fi
print_info "Last deployment changed to $DEPLOYED_SHA1";
}
remote_lock() {
[ $ENABLE_REMOTE_LCK -ne 1 ] && return
[ $FORCE -ne 1 ] && check_remote_lock
local LCK_MESSAGE="${USER}@$(hostname --fqdn) on $(date --utc --rfc-2822)"
local LOCAL_SHA1="$(git log -n 1 --pretty=format:%H)"
write_log "Remote locking $LCK_MESSAGE"
if [ $DRY_RUN -ne 1 ]; then
echo "${LOCAL_SHA1}\n${LCK_MESSAGE}" | upload_file - $REMOTE_LCK_FILE
check_exit_status "Could not upload remote lock file" $ERROR_UPLOAD
fi
}
release_remote_lock() {
[ $ENABLE_REMOTE_LCK != 1 ] && return;
write_log "Releasing remote lock"
remove_file $REMOTE_LCK_FILE
}
lock() {
if [ $(uname | grep 'MINGW') ]; then
write_log "Ingnoring lock on MinGW"
return
fi
# Checks locking, make sure this only run once a time
if [ -f "$LCK_FILE" ]; then
# The file exists so read the PID to see if it is still running
local MYPID=$(head -n 1 "$LCK_FILE")
local TEST_RUNNING=$(ps -p $MYPID | grep $MYPID)
if [ -z "$TEST_RUNNING" ]; then
# The process is not running echo current PID into lock file
write_log "Not running yet, locking"
echo $$ > "$LCK_FILE"
else
write_log "$(basename $0) is already running [$MYPID]"
exit 0
fi
else
write_log "Not running yet, locking"
echo $$ > "$LCK_FILE"
fi
}
# Release lock func
release_lock() {
if [ $(uname | grep 'MINGW') ]; then
write_log "Ingnoring lock on MinGW"
return
fi
write_log "Releasing lock"
rm -f "$LCK_FILE"
}
set_remote_host() {
[ -z $URL ] && [ -n "$SCOPE" ] && URL="$(git config --get git-ftp.$SCOPE.url)"
[ -z $URL ] && URL="$(git config --get git-ftp.url)"
[ -z $REMOTE_CACERT ] && [ -n "$SCOPE" ] && REMOTE_CACERT="$(git config --get git-ftp.$SCOPE.cacert)"
[ -z $REMOTE_CACERT ] && REMOTE_CACERT="$(git config --get git-ftp.cacert)"
REMOTE_HOST=$(expr "$URL" : ".*://\([[:alpha:]0-9\.:-]*\).*")
[ -z $REMOTE_HOST ] && REMOTE_HOST=$(expr "$URL" : "\([[:alpha:]0-9\.:-]*\).*")
[ -z $REMOTE_HOST ] && print_error_and_die "Remote host not set" $ERROR_MISSING_ARGUMENTS
}
set_remote_protocol() {
# Split protocol from url
REMOTE_PROTOCOL=$(echo "$URL" | tr '[:upper:]' '[:lower:]' | egrep '^(ftp|sftp|ftps|ftpes)://.*' | cut -d ':' -f 1)
# Protocol found?
if [ ! -z $REMOTE_PROTOCOL ]; then
REMOTE_PATH=$(echo "$URL" | cut -d '/' -f 4-)
handle_remote_protocol_options
return
fi
# Check if a unknown protocol is set, handle it or use default protocol
local UNKNOWN_PROTOCOL=$(expr "$URL" : "\(.*:[/]*\).*")
if [ -z $UNKNOWN_PROTOCOL ]; then
write_log "Protocol not set, using default protocol $DEFAULT_PROTOCOL://"
REMOTE_PROTOCOL=$DEFAULT_PROTOCOL
[ $(echo "$URL" | egrep "/" | wc -l) -ne 0 ] && REMOTE_PATH=$(echo "$URL" | cut -d '/' -f 2-)
handle_remote_protocol_options
return
fi
print_error_and_die "Protocol unknown '$UNKNOWN_PROTOCOL'" $ERROR_UNKNOWN_PROTOCOL
}
set_deployed_sha1() {
local local_sha1=$(git log -n 1 --pretty=format:%H)
# Return if commit is set by user interaction using --commit
if [ -n "$DEPLOYED_SHA1" ]; then
return
fi
# Get the last commit (SHA) we deployed if not ignored or not found
write_log "Retrieving last commit from $REMOTE_PROTOCOL://$REMOTE_HOST/$REMOTE_PATH"
DEPLOYED_SHA1="$(get_file_content $DEPLOYED_SHA1_FILE)"
check_exit_status "Could not get last commit. Network down? Wrong URL? Use 'git ftp init' for the inital push." $ERROR_DOWNLOAD
write_log "Last deployed SHA1 for $REMOTE_HOST/$REMOTE_PATH is $DEPLOYED_SHA1"
if [ $UNCOMMITED -ne 0 -o $UNSTAGED -ne 0 ]; then
if [ "$local_sha1" != "$DEPLOYED_SHA1" ]; then
print_error_and_die "Some commits haven't been deployed."
fi
fi
}
set_changed_files() {
# Get raw list of files
if [ $IGNORE_DEPLOYED -ne 0 ]; then
git ls-files -t > '.git-ftp-tmp'
elif [ $UNSTAGED -ne 0 ]; then
git diff --name-status 2>/dev/null > '.git-ftp-tmp'
elif [ $UNCOMMITED -ne 0 ]; then
git diff --name-status HEAD 2>/dev/null > '.git-ftp-tmp'
else
git diff --name-status $DEPLOYED_SHA1 2>/dev/null > '.git-ftp-tmp'
fi
# Filter against ignore file
if [ -f '.git-ftp-ignore' ]; then
FILES_CHANGED=$(grep --invert-match -f '.git-ftp-ignore' '.git-ftp-tmp' | tr '\t' ' ')
else
FILES_CHANGED=$(cat '.git-ftp-tmp' | tr '\t' ' ')
fi
rm '.git-ftp-tmp'
if [ $IGNORE_DEPLOYED -ne 0 ]; then
write_log "Sync all files."
return
fi
if [ $? -ne 0 ]; then
if [ $FORCE -ne 1 ]; then
print_info "Unknown SHA1 object, make sure you are deploying the right branch and it is up-to-date."
echo -n "Do you want to ignore and upload all files again? [y/N] "
read ANSWER_STATE
if [ "$ANSWER_STATE" != "y" ] && [ "$ANSWER_STATE" != "Y" ]; then
print_info "Aborting..."
release_lock
exit 0
else
write_log "Taking all files";
FILES_CHANGED="$(git ls-files -t)"
fi
else
print_info "Unknown SHA1 object, could not determine changed filed, taking all files"
FILES_CHANGED="$(git ls-files -t)"
fi
elif [ -n "$FILES_CHANGED" ]; then
write_log "Having changed files";
else
print_info "No changed files for $REMOTE_HOST/$REMOTE_PATH. Everything up-to-date."
release_lock
exit 0
fi
}
handle_file_sync() {
# Calculate total file count
local DONE_ITEMS=0
local TOTAL_ITEMS=$(echo "$FILES_CHANGED" | wc -l)
TOTAL_ITEMS=$((TOTAL_ITEMS+0)) # trims whitespaces produced by wc
write_log "There are $TOTAL_ITEMS files to upload"
# Changing internal field separator, file names could have spaces
OIFS="$IFS"
# dash does not like NIFS=$'\n', using WYSIWYG
NIFS=$'
'
IFS="$NIFS"
for FILE_ITERATOR in $FILES_CHANGED; do
(( DONE_ITEMS++ ))
FILE_MODE=$(echo "$FILE_ITERATOR" | cut -f1 -d ' ')
FILE_NAME=$(echo "$FILE_ITERATOR" | cut -f2- -d ' ')
if [ "$FILE_MODE" != "D" ]; then
print_info "[$DONE_ITEMS of $TOTAL_ITEMS] Uploading '$FILE_NAME'"
if is_submodule $FILE_NAME; then
handle_submodule_sync $FILE_NAME
else
[ $DRY_RUN -ne 1 ] && upload_file $FILE_NAME
fi
else
# Removing file
print_info "[$DONE_ITEMS of $TOTAL_ITEMS] Removing '$FILE_NAME'"
[ $DRY_RUN -ne 1 ] && remove_file $FILE_NAME && remove_dir $(dirname $FILE_NAME)
fi
done
IFS="$OIFS"
}
handle_submodule_sync() {
print_info "Handling submodule sync for $1"
# Duplicate the current required parameters
args=(--user "$REMOTE_USER" --passwd "$REMOTE_PASSWD")
# Do not ask any questions for submodules
args+=(--force)
[ $IGNORE_DEPLOYED -eq 1 ] && args+=(--all)
if [ $VERBOSE -eq 1 ]; then
args+=(--verbose)
else
args+=(--silent)
fi
[ $DRY_RUN -eq 1 ] && args+=(--dry-run)
(
cd $1 && $0 $ACTION ${args[@]} $REMOTE_PROTOCOL://$REMOTE_HOST/${REMOTE_PATH}$1
)
local EXIT_CODE=$?
# Pushing failed. Submodule may not be initialized
if [ $EXIT_CODE -eq $ERROR_DOWNLOAD ] && [ "$ACTION" == "push" ]; then
print_info "Could not push $1, trying to init..."
(
cd $1 && $0 init ${args[@]} $REMOTE_PROTOCOL://$REMOTE_HOST/${REMOTE_PATH}$1
)
check_exit_status "Failed to sync submodules" $ERROR_UPLOAD
elif [ $EXIT_CODE -ne 0 ]; then
print_error_and_die "Failed to sync submodules" $ERROR_UPLOAD
fi
}
handle_remote_protocol_options() {
# SFTP uses a different remove command and uses absolute paths
[ "$REMOTE_PROTOCOL" = "sftp" ] && REMOTE_DELETE_CMD="rm /"
[ "$REMOTE_PROTOCOL" = "ftpes" -o "$REMOTE_PROTOCOL" = "ftps" ] && \
[ -n "$REMOTE_CACERT" -a -r "$REMOTE_CACERT" ] && \
REMOTE_CMD_OPTIONS="$REMOTE_CMD_OPTIONS --cacert $REMOTE_CACERT"
# Options for curl if using FTPES
[ "$REMOTE_PROTOCOL" = "ftpes" ] && REMOTE_PROTOCOL="ftp" && REMOTE_CMD_OPTIONS="$REMOTE_CMD_OPTIONS --ssl"
# Require users' explicit consent for insecure connections
[ $CURL_INSECURE = 1 ] && REMOTE_CMD_OPTIONS="$REMOTE_CMD_OPTIONS -k"
}
handle_action() {
case "$ACTION" in
init)
action_init
;;
push)
action_push
;;
catchup)
action_catchup
;;
show)
action_show
;;
*)
print_error_and_die "Action unknown" $ERROR_MISSING_ARGUMENTS
;;
esac
}
set_remote_user() {
[ -z $REMOTE_USER ] && [ -n "$SCOPE" ] && REMOTE_USER="$(git config --get git-ftp.$SCOPE.user)"
[ -z $REMOTE_USER ] && REMOTE_USER="$(git config --get git-ftp.user)"
}
set_remote_password() {
[ -z $REMOTE_PASSWD ] && [ $USE_KEYCHAIN -eq 1 ] && get_keychain_password "$KEYCHAIN_USER"
[ -z $REMOTE_PASSWD ] && [ -n "$SCOPE" ] && REMOTE_PASSWD="$(git config --get git-ftp.$SCOPE.password)"
[ -z $REMOTE_PASSWD ] && REMOTE_PASSWD="$(git config --get git-ftp.password)"
}
set_remotes() {
set_remote_host
write_log "Host is '$REMOTE_HOST'"
set_remote_user
write_log "User is '$REMOTE_USER'"
set_remote_password
if [ -z $REMOTE_PASSWD ]; then
write_log "No password is set"
else
write_log "Password is set"
fi
set_remote_protocol
# Add trailing slash if missing
if [ ! -z $REMOTE_PATH ] && [ $(echo "$REMOTE_PATH" | egrep "*/$" | wc -l) -ne 1 ]; then
write_log "Added missing trailing / in path"
REMOTE_PATH="$REMOTE_PATH/"
fi
write_log "Path is '$REMOTE_PATH'"
}
# ------------------------------------------------------------
# Actions
# ------------------------------------------------------------
action_init() {
check_git_version
check_is_git_project
check_is_dirty_repository
set_remotes
lock
check_deployed_sha1
set_changed_files
remote_lock
handle_file_sync
upload_local_sha1
release_remote_lock
release_lock
}
action_push() {
check_git_version
check_is_git_project
[ $UNCOMMITED -eq 0 -a $UNSTAGED -eq 0 ] && check_is_dirty_repository
set_remotes
lock
set_deployed_sha1
set_changed_files
remote_lock
handle_file_sync
[ $UNCOMMITED -eq 0 -a $UNSTAGED -eq 0 ] && upload_local_sha1
release_remote_lock
release_lock
}
action_catchup() {
check_is_git_project
check_is_dirty_repository
set_remotes
lock
upload_local_sha1
release_lock
}
action_show() {
set_remotes
DEPLOYED_SHA1="$(get_file_content $DEPLOYED_SHA1_FILE)"
check_exit_status "Could not get uploaded log file" $ERROR_DOWNLOAD
git show "$DEPLOYED_SHA1"
}
# ------------------------------------------------------------
# Checks
# ------------------------------------------------------------
check_deployed_sha1() {
write_log "Check if $REMOTE_PROTOCOL://$REMOTE_HOST/$REMOTE_PATH is clean"
DEPLOYED_SHA1="$(get_file_content $DEPLOYED_SHA1_FILE)"
if [ "$DEPLOYED_SHA1" != "" ]; then
print_error_and_die "Commit found, use 'git ftp push' to sync. Exiting..." $ERROR_USAGE
fi
# Make sure if sync all files if no sha1 was found
IGNORE_DEPLOYED=1
}
check_git_version() {
local GIT_VERSION=$(git --version | cut -d ' ' -f 3)
local MAJOR=$(echo $GIT_VERSION | cut -d '.' -f 1)
local MINOR=$(echo $GIT_VERSION | cut -d '.' -f 2)
if [ $MAJOR -lt 2 ] && [ $MINOR -lt 7 ]; then
print_error_and_die "Git is too old, 1.7.0 or higher suported only" $ERROR_GIT
fi
}
check_remote_lock() {
write_log "Checking remote lock"
local LCK_CONTENT="$(get_file_content $REMOTE_LCK_FILE 2>/dev/null)"
if [ -n "$LCK_CONTENT" ]; then
local LCK_SHA1=$(echo "$LCK_CONTENT" | head -n 1)
write_log "Remote lock sha1 $LCK_SHA1"
local LOCAL_SHA1="$(git log -n 1 --pretty=format:%H)"
write_log "Local sha1 $LOCAL_SHA1"
if [ "$LCK_SHA1" != "$LOCAL_SHA1" ]; then
local LCK_USER=$(echo "$LCK_CONTENT" | tail -n 1)
print_error_and_die "Remote locked by $LCK_USER" $ERROR_REMOTE_LOCKED
fi
fi
}
check_is_git_project() {
[ -d ".git" ] || print_error_and_die "Not a Git project? Exiting..." $ERROR_GIT
}
check_is_dirty_repository() {
local CLEAN_REPO=$(git status -uno | egrep "nothing to commit*" | wc -l)
[ $CLEAN_REPO -ne 1 ] && print_error_and_die "Dirty repository: Having uncommitted changes. Exiting..." $ERROR_GIT
}
# ------------------------------------------------------------
# Main
# ------------------------------------------------------------
main() {
handle_action
exit 0
}
# 2 args are needed: action and url
if [ $# = 0 ]; then
usage;
fi
while test $# != 0
do
case "$1" in
init|push|catchup|show)
ACTION="$1"
;;
-h|--h|--he|--hel|--help|help)
usage_long
;;
-u|--user*)
case "$#,$1" in
*,*=*)
REMOTE_USER=$(expr "z$1" : 'z-[^=]*=\(.*\)')
;;
1,*)
REMOTE_USER="$USER"
;;
*)
if [ ! $(echo "$2" | egrep '^-' | wc -l) -eq 1 ]; then
REMOTE_USER="$2"
shift
else
REMOTE_USER="$USER"
fi
;;
esac
;;
-s|--scope*)
case "$#,$1" in
*,*=*)
SCOPE=$(expr "z$1" : 'z-[^=]*=\(.*\)')
;;
1,*)
print_error_and_die "Too few arguments for option -s" $ERROR_MISSING_ARGUMENTS
;;
*)
if [ ! $(echo "$2" | egrep '^-' | wc -l) -eq 1 ]; then
SCOPE="$2"
shift
else
print_error_and_die "Too few arguments for option -s" $ERROR_MISSING_ARGUMENTS
fi
;;
esac
write_log "Using scope $SCOPE if available"
;;
-c|--commit*)
case "$#,$1" in
*,*=*)
DEPLOYED_SHA1=$(expr "z$1" : 'z-[^=]*=\(.*\)')
;;
1,*)
print_error_and_die "Too few arguments for option -c" $ERROR_MISSING_ARGUMENTS
;;
*)
if [ ! $(echo "$2" | egrep '^-' | wc -l) -eq 1 ]; then
DEPLOYED_SHA1="$2"
shift
else
print_error_and_die "Too few arguments for option -c" $ERROR_MISSING_ARGUMENTS
fi
;;
esac
write_log "Using commit $DEPLOYED_SHA1 as deployed"
;;
-p|--passwd*)
case "$#,$1" in
*,*=*)
REMOTE_PASSWD=$(expr "z$1" : 'z-[^=]*=\(.*\)')
;;
1,*)
ask_for_passwd
;;
*)
if [ ! $(echo "$2" | egrep '^-' | wc -l) -eq 1 ]; then
REMOTE_PASSWD="$2"
shift
else
ask_for_passwd
fi
;;
esac
;;
-k|--keychain*)
USE_KEYCHAIN=1
write_log "Enabled keychain"
case "$#,$1" in
*,*=*)
KEYCHAIN_USER=$(expr "z$1" : 'z-[^=]*=\(.*\)')
;;
1,*)
# Nothing is handed over, this is okay
;;
*)
if [ ! $(echo "$2" | egrep '^-' | wc -l) -eq 1 ]; then
KEYCHAIN_USER="$2"
shift
fi
;;
esac
;;
-a|--all)
IGNORE_DEPLOYED=1
;;
-l|--lock)
if [ $ENABLE_REMOTE_LCK -ne 1 ]; then
write_log "Enabling remote locking feature"
ENABLE_REMOTE_LCK=1
else
write_log "Disabling remote locking feature"
ENABLE_REMOTE_LCK=0
fi
;;
-D|--dry-run)
DRY_RUN=1
write_log "Running dry, won't do anything"
;;
-n|--silent)
VERBOSE=-1
REMOTE_CMD_OPTIONS="-s"
;;
-v|--verbose)
VERBOSE=1
[ -n "$LOG_CACHE" ] && echo -e $LOG_CACHE
REMOTE_CMD_OPTIONS=""
;;
-vv)
VERBOSE=1
[ -n "$LOG_CACHE" ] && echo -e $LOG_CACHE
REMOTE_CMD_OPTIONS="-v"
;;
--insecure)
CURL_INSECURE=1
write_log "Insecure SSL/TLS connection allowed"
;;
--cacert*)
case "$#,$1" in
*,*=*)
REMOTE_CACERT=$(expr "z$1" : 'z-[^=]*=\(.*\)')
;;
1,*)
print error_and_die "Too few arguments for option --cacert" $ERROR_MISSING_ARGUMENTS
;;
*)
if [ ! $(echo "$2" | egrep '^-' | wc -l) -eq 1 ]; then
REMOTE_CACERT="$2"
shift
else
print_error_and_die "Too few arguments for option --cacert" $ERROR_MISSING_ARGUMENTS
fi
;;
esac
if [ ! $(echo "$REMOTE_CACERT" | egrep '^/' | wc -l) -eq 1 ]; then
REMOTE_CACERT="$STARTDIR/$REMOTE_CACERT"
fi
;;
-f|--force)
FORCE=1
write_log "Forced mode enabled"
;;
--version|version)
echo "git-ftp version $VERSION"
exit 0
;;
-A|--active)
ACTIVE_MODE=1
write_log "Using active mode"
;;
--unstaged)
UNSTAGED=1
write_log "Pushing unstaged files"
;;
--uncommited)
UNCOMMITED=1
write_log "Pushing uncommited files"
;;
*)
# Pass thru anything that may be meant for fetch.
[ -n "$1" ] && URL=$1
;;
esac
shift
done
main