#
# Tests for merging with renames
#
#

proc commit_id {version} {
  regexp -line {^artifact:\s+(\S+)} [fossil whatis $version] - id
  return $id
}

require_no_open_checkout

######################################
#  Test 1                            #
#  Reported: Ticket [554f44ee74e3d]  #
######################################

test_setup

write_file f1 "line"
fossil add f1
fossil commit -m "c1"
fossil tag add pivot current

write_file f1 "line2"
fossil commit -m "c2"

write_file f1 "line3"
fossil commit -m "c3"

write_file f1 "line4"
fossil commit -m "c4"

write_file f1 "line5"
fossil commit -m "c4"

write_file f1 "line6"
fossil commit -m "c4"

fossil update pivot
fossil mv --hard f1 f2
fossil commit -b rename -m "c5"

fossil merge trunk
fossil commit -m "trunk merged"

fossil update pivot
write_file f3 "someline"
fossil add f3
fossil commit -b branch2 -m "newbranch"

fossil merge trunk
test_status_list merge_renames-1 $RESULT {UPDATE f1}

######################################
#  Test 2                            #
#  Reported: Ticket [74413366fe5067] #
######################################

test_setup

write_file f1 "line"
fossil add f1
fossil commit -m "base file"
fossil tag add pivot current

write_file f2 "line2"
fossil add f2
fossil commit -m "newfile"

fossil mv --hard f2 f2new
fossil commit -m "rename"

fossil update pivot
write_file f1 "line3"
fossil commit -b branch -m "change"

fossil merge trunk
fossil commit -m "trunk merged"

fossil update trunk

fossil merge branch
test_status_list merge_renames-2 $RESULT {UPDATE f1}

######################################
#  Test 3                            #
#  Reported: Ticket [30b28cf351]     #
######################################

test_setup

write_file f1 "line"
fossil add f1
fossil commit -m "base file"
fossil tag add pivot current

write_file f2 "line2"
fossil add f2
fossil commit -m "newfile"

fossil mv --hard f2 f2new
fossil commit -m "rename"

fossil update pivot
write_file f1 "line3"
fossil commit -b branch -m "change"

fossil merge trunk
fossil commit -m "trunk merged"

fossil update trunk

fossil merge branch
test_status_list merge_renames-3 $RESULT {UPDATE f1}

######################################
#  Test 4                            #
#  Reported: Ticket [67176c3aa4]     #
######################################

test_setup

write_file f1 "f1"
fossil add f1
fossil commit -m "add f1"

write_file f1 "f1.1"
fossil commit --branch b -m "change f1"

fossil update trunk
fossil mv --hard f1 f2
fossil commit -m "f1 -> f2"

fossil merge b
test_status_list merge_renames-4-1 $RESULT {UPDATE f2}
test_file_contents merge_renames-4-2 f2 "f1.1"

######################################
#  Test 5                            #
#  Handle Rename/Add via Merge       #
######################################

test_setup

write_file f1 "old f1 line"
fossil add f1
fossil commit -m "base file"

write_file f3 "f3 line"
fossil add f3
fossil commit -m "branch file" -b branch_for_f3

fossil update trunk
fossil mv --hard f1 f2
write_file f1 "new f1 line"
fossil add f1
fossil commit -m "rename and add file with old name"

fossil update branch_for_f3
fossil merge trunk
test_status_list merge_renames-5-1 $RESULT {
  RENAME f1 -> f2
  ADDED f1
}

fossil commit -m "trunk merged, should have 3 files"

fossil ls

test merge_renames-5-2 {[normalize_result] eq {f1
f2
f3}}

#####################################
#  Test 6                           #
#  Merging a branch multiple times  #
#####################################

test_setup

write_file f1 "f1"
fossil add f1
fossil commit -m "add f1"

fossil mv --hard f1 f2
fossil commit -b b -m "f1 -> f2"

fossil update trunk
write_file f3 "f3"
write_file f4 "f4"
fossil add f3 f4
fossil ci -m "add f3, f4"

fossil mv --hard f3 f3-old
fossil mv --hard f4 f3
fossil mv --hard f3-old f4
fossil ci -m "swap f3 and f4"

write_file f1 "f1.1"
fossil commit -m "edit f1"

fossil update b
fossil merge trunk
fossil commit -m "merge trunk"

fossil update trunk
write_file f1 "f1.2"
write_file f3 "f3.1"
write_file f4 "f4.1"
fossil commit -m "edit f1, f4"

fossil update b
fossil merge trunk
test_status_list merge_renames-6-1 $RESULT {
  UPDATE f2
  UPDATE f3
  UPDATE f4
}
test_file_contents merge_renames-6-2 f2 "f1.2"
test_file_contents merge_renames-6-3 f3 "f3.1"
test_file_contents merge_renames-6-4 f4 "f4.1"

########################################################################
#  Test 7                                                              #
#  Merging with an uncommitted rename of a file that has been renamed  #
#  in the merged branch and adding a new file with the original name   #
########################################################################

test_setup

write_file f1 "f1"
fossil add f1
fossil commit -m "add f1"

fossil mv --hard f1 f2
write_file f2 "f2"
fossil commit -b b -m "f1 -> f2, edit f2"

fossil update trunk
fossil mv --hard f1 f3
write_file f1 "f1.1"
fossil add f1
fossil merge b
test_status_list merge_renames-7-1 $RESULT {UPDATE f3}
test_file_contents merge_renames-7-2 f1 "f1.1"
test_file_contents merge_renames-7-3 f3 "f2"

######################################################
#  Test 8                                            #
#  Merging two branches that both add the same file  #
######################################################

test_setup

write_file f1 "f1.1"
fossil add f1
fossil commit -b b1 -m "add f1"

fossil update trunk
write_file f1 "f1.2"
fossil add f1
fossil commit -b b2 -m "add f1"

fossil update trunk
fossil merge b1
fossil merge b2 -expectError
test_status_list merge_renames-8-1 $RESULT {
  MERGE f1
  WARNING: 1 merge conflicts
}

fossil revert
fossil merge --integrate b1
fossil merge b2 -expectError
test_status_list merge_renames-8-2 $RESULT {
  MERGE f1
  WARNING: 1 merge conflicts
}

#############################################
#  Test 9                                   #
#  Merging a delete/rename/add combination  #
#############################################

test_setup

write_file f1 "f1"
write_file f2 "f2"
fossil add f1 f2
fossil commit -m "add files"

fossil rm --hard f2
fossil commit -b b -m "delete f2"

fossil mv --hard f1 f2
fossil commit -m "f1 -> f2"

write_file f1 "f1.1"
fossil add f1
fossil commit -m "add new f1"

fossil update trunk
fossil merge b
set expectedMerge {
  DELETE f2
  RENAME f1 -> f2
  ADDED f1
}
test_status_list merge_renames-9-1 $RESULT $expectedMerge
fossil changes
test_status_list merge_renames-9-2 $RESULT "
  MERGED_WITH [commit_id b]
  ADDED_BY_MERGE f1
  RENAMED f1  ->  f2
  DELETED f2  ->  f2 (overwritten by rename)
"
test_file_contents merge_renames-9-3 f1 "f1.1"
test_file_contents merge_renames-9-4 f2 "f1"

# Undo and ensure a dry run merge results in no changes
fossil undo
test_status_list merge_renames-9-5 $RESULT {
  UNDO f1
  UNDO f2
}
fossil merge -n b -expectError
test_status_list merge_renames-9-6 $RESULT "
  $expectedMerge
  REMINDER: this was a dry run - no files were actually changed.
"
test merge_renames-9-7 {[fossil changes] eq ""}

###################################################################
#  Test 10                                                        #
#  Merge swapped filenames, backout the swap, then merge changes  #
###################################################################

test_setup

write_file f1 "f1"
write_file f2 "f2"
fossil add f1 f2
fossil commit -m "add files" ;# N

fossil mv --hard f1 f1-tmp
fossil mv --hard f2 f1
fossil mv --hard f1-tmp f2
fossil commit -b b -m "swap f1, f2" ;# P

fossil update trunk
fossil merge b
test_status_list merge_renames-10-1 $RESULT {
  RENAME f1 -> f2
  RENAME f2 -> f1
}
test_file_contents merge_renames-10-2 f1 "f2"
test_file_contents merge_renames-10-3 f2 "f1"
fossil commit -m "merge b"

fossil update b
write_file f1 f1.1
write_file f2 f2.1
fossil commit -m "edit" ;# M

fossil update trunk
fossil merge --backout trunk
test_status_list merge_renames-10-4 $RESULT {
  RENAME f1 -> f2
  RENAME f2 -> f1
}
test_file_contents merge_renames-10-5 f1 "f1"
test_file_contents merge_renames-10-6 f2 "f2"
test_status_list merge_renames-10-7 [fossil changes] "
  RENAMED f1  ->  f2
  RENAMED f2  ->  f1
  BACKOUT [commit_id trunk]
"
fossil commit -m "swap back" ;# V

fossil merge b
test_status_list merge_renames-10-8 $RESULT {
  UPDATE f1
  UPDATE f2
}

test_file_contents merge_renames-10-9 f1 "f2.1"
test_file_contents merge_renames-10-10 f2 "f1.1"

############################################
#  Test 11                                 #
#  Specifying a baseline                   #
############################################

test_setup

write_file f1 "line"
fossil add f1
fossil commit -m "add f1"

write_file f1 "line\nline2"
fossil commit -b b -m "edit f2" --tag p1

fossil mv --hard f1 f2
fossil commit -m "f1 -> f2"

write_file f2 "line\nline2\nline3"
fossil commit -m "edit f2" --tag p2

write_file f2 "line\nline2\nline3\nline4"
fossil commit -m "edit f2"

fossil update trunk
fossil merge --baseline p1 b
test_status_list merge_renames-11-1 $RESULT {
  MERGE f1
  RENAME f1 -> f2
}
test_file_contents merge_renames-11-2 f2 "line\nline3\nline4"
fossil revert
fossil merge --baseline p2 b
test_status_list merge_renames-11-3 $RESULT {MERGE f1}
test_file_contents merge_renames-11-4 f1 "line\nline4"

#################################################################
#  Test 12                                                      #
#  Merge involving a pivot that isn't a first-parent ancestor   #
#  of either the checked-out commit or the commit being merged  #
#################################################################

test_setup

write_file f1 "f1\n"
fossil add f1
fossil commit -m "add f1" --tag n

fossil mv --hard f1 f1n
fossil commit -m "f1 -> f1n"

fossil mv --hard f1n f1v
write_file f1v "f1v\n"
fossil commit -b v -m "f1n -> f1v, edit f1v"

fossil update trunk
fossil mv --hard f1n f1m
fossil commit -b m -m "f1n -> f1m"

fossil update n
fossil mv --hard f1 f1p
write_file f1p "f1\np"
fossil commit -b p -m "f1 -> f1p, edit f1p"

fossil update m
fossil merge p
test_status_list merge_renames-12-1 $RESULT {UPDATE f1m}
test_file_contents merge_renames-12-2 f1m "f1\np"
fossil commit -m "merge p"

write_file f1m "f1\nm"
fossil commit -m "edit f1m"

fossil update v
fossil merge p
test_status_list merge_renames-12-3 $RESULT {MERGE f1v}
test_file_contents merge_renames-12-4 f1v "f1v\np"
fossil commit -m "merge p"

fossil merge m
test_status_list merge_renames-12-5 $RESULT {MERGE f1v}
test_file_contents merge_renames-12-6 f1v "f1v\nm"
fossil commit -m "merge m"

#################################################################
#  Test 13                                                      #
#  Merge in add+rename that happened on a branch, then merged   #
#  back to branch, and again to trunk, then merge in branch.    #
#################################################################

set repoDir [test_setup]
write_file f1 "line1"
fossil add f1
fossil commit -m "add f1" --tag c1

write_file f2 "line1"
fossil add f2
fossil commit -m "add f2 on branch" -b b --tag c2
fossil update trunk
test_status_list merge_renames_13-1 $RESULT {REMOVE f2}
write_file f1 "line1\nline2\n"
fossil commit -m "edit f1 on trunk" --tag c3

fossil update b
test_status_list merge_renames_13-2 $RESULT {
  UPDATE f1
  ADD f2
}
fossil merge trunk
fossil commit -m "merge trunk" --tag c4
fossil mv --hard f2 f2n
test_status_list merge_renames-13-3 $RESULT "
  RENAME f2 f2n
  MOVED_FILE [file normalize $repoDir]/f2
"
fossil commit -m "renamed f2->f2n" --tag c5

fossil update trunk
fossil merge b
test_status_list merge_renames-13-4 $RESULT {ADDED f2n}
fossil commit -m "merge f2n" --tag m1 --tag c6

fossil update b
write_file f1 "line1\nline3\nline2"
fossil commit -m "edit f1 on b" --tag c7

fossil update trunk
write_file f1 "line1\nline3\nline2\nline4"
fossil commit -m "edit f1 on trunk" --tag c8

fossil update b
fossil merge trunk
test_status_list merge_renames-13-5 $RESULT {MERGE f1}
fossil commit -m "merge trunk" --tag c9
write_file f1 "line1\nline3\nline4"
fossil commit -m "edit f1 on b" --tag c10

fossil update m1
fossil merge b
test_status_list merge_renames-13-6 $RESULT {
  UPDATE f1
}
test_file_contents merge_renames-13-7 f2n "line1"

fossil revert
test_status_list merge_renames-13-8 $RESULT {
  REVERT f1
}
fossil update trunk
fossil merge --integrate b
test_status_list merge_renames-13-9 $RESULT {
  UPDATE f1
}
test_file_contents merge_renames-13-10 f2n "line1"

fossil revert
test_status_list merge_renames-13-11 $RESULT {
  REVERT f1
}

######################################
#
# Tests for troubles not specifically linked with renames but that I'd like to
# write:
#  [c26c63eb1b] - 'merge --backout' does not handle conflicts properly
#  [953031915f] - Lack of warning when overwriting extra files
#  [4df5f38f1e] - Troubles merging a file delete with a file change

###############################################################################

test_cleanup
