Imported Upstream version 2.5.1
[scm/test.git] / t / t-migrate-import-no-rewrite.sh
1 #!/usr/bin/env bash
2
3 . "$(dirname "$0")/fixtures/migrate.sh"
4 . "$(dirname "$0")/testlib.sh"
5
6 begin_test "migrate import --no-rewrite (default branch)"
7 (
8   set -e
9
10   setup_local_branch_with_gitattrs
11
12   txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
13   prev_commit_oid="$(git rev-parse HEAD)"
14
15   git lfs migrate import --no-rewrite --yes *.txt
16
17   # Ensure our desired files were imported into git-lfs
18   assert_pointer "refs/heads/master" "a.txt" "$txt_oid" "120"
19   assert_local_object "$txt_oid" "120"
20
21   # Ensure the git history remained the same
22   new_commit_oid="$(git rev-parse HEAD~1)"
23   if [ "$prev_commit_oid" != "$new_commit_oid" ]; then
24     exit 1
25   fi
26
27   # Ensure a new commit was made
28   new_head_oid="$(git rev-parse HEAD)"
29   if [ "$prev_commit_oid" == "$new_oid" ]; then
30     exit 1
31   fi
32
33   # Ensure a new commit message was generated based on the list of imported files
34   commit_msg="$(git log -1 --pretty=format:%s)"
35   echo "$commit_msg" | grep -q "a.txt: convert to Git LFS"
36 )
37 end_test
38
39 begin_test "migrate import --no-rewrite (bare repository)"
40 (
41   set -e
42
43   setup_single_remote_branch_with_gitattrs
44
45   prev_commit_oid="$(git rev-parse HEAD)"
46   txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
47   md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
48
49   git lfs migrate import --no-rewrite --yes a.txt a.md
50
51   # Ensure our desired files were imported
52   assert_pointer "refs/heads/master" "a.txt" "$txt_oid" "30"
53   assert_pointer "refs/heads/master" "a.md" "$md_oid" "50"
54
55   # Ensure the git history remained the same
56   new_commit_oid="$(git rev-parse HEAD~1)"
57   if [ "$prev_commit_oid" != "$new_commit_oid" ]; then
58     exit 1
59   fi
60
61   # Ensure a new commit was made
62   new_head_oid="$(git rev-parse HEAD)"
63   if [ "$prev_commit_oid" == "$new_oid" ]; then
64     exit 1
65   fi
66 )
67 end_test
68
69 begin_test "migrate import --no-rewrite (multiple branches)"
70 (
71   set -e
72
73   setup_multiple_local_branches_with_gitattrs
74
75   prev_commit_oid="$(git rev-parse HEAD)"
76
77   md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
78   txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
79   md_feature_oid="$(calc_oid "$(git cat-file -p my-feature:a.md)")"
80
81   git lfs migrate import --no-rewrite --yes *.txt *.md
82
83   # Ensure our desired files were imported
84   assert_pointer "refs/heads/master" "a.md" "$md_oid" "140"
85   assert_pointer "refs/heads/master" "a.txt" "$txt_oid" "120"
86
87   assert_local_object "$md_oid" "140"
88   assert_local_object "$txt_oid" "120"
89
90   # Ensure our other branch was unmodified
91   refute_local_object "$md_feature_oid" "30"
92
93   # Ensure the git history remained the same
94   new_commit_oid="$(git rev-parse HEAD~1)"
95   if [ "$prev_commit_oid" != "$new_commit_oid" ]; then
96     exit 1
97   fi
98
99   # Ensure a new commit was made
100   new_head_oid="$(git rev-parse HEAD)"
101   if [ "$prev_commit_oid" == "$new_oid" ]; then
102     exit 1
103   fi
104 )
105 end_test
106
107 begin_test "migrate import --no-rewrite (no .gitattributes)"
108 (
109   set -e
110
111   setup_multiple_local_branches
112
113   # Ensure command fails if no .gitattributes files are present
114   git lfs migrate import --no-rewrite --yes *.txt *.md 2>&1 | tee migrate.log
115   if [ ${PIPESTATUS[0]} -eq 0 ]; then
116     echo >&2 "fatal: expected git lfs migrate import --no-rewrite to fail, didn't"
117     exit 1
118   fi
119
120   grep "no Git LFS filters found in .gitattributes" migrate.log
121 )
122 end_test
123
124 begin_test "migrate import --no-rewrite (nested .gitattributes)"
125 (
126   set -e
127
128   setup_local_branch_with_nested_gitattrs
129
130   # Ensure a .md filter does not exist in the top-level .gitattributes
131   master_attrs="$(git cat-file -p "$master:.gitattributes")"
132   [ !"$(echo "$master_attrs" | grep -q ".md")" ]
133
134   # Ensure a .md filter exists in the nested .gitattributes
135   nested_attrs="$(git cat-file -p "$master:b/.gitattributes")"
136   echo "$nested_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
137
138   md_oid="$(calc_oid "$(git cat-file -p :a.md)")"
139   nested_md_oid="$(calc_oid "$(git cat-file -p :b/a.md)")"
140   txt_oid="$(calc_oid "$(git cat-file -p :a.txt)")"
141
142   git lfs migrate import --no-rewrite --yes a.txt b/a.md
143
144   # Ensure a.txt and subtree/a.md were imported, even though *.md only exists in the
145   # nested subtree/.gitattributes file
146   assert_pointer "refs/heads/master" "b/a.md" "$nested_md_oid" "140"
147   assert_pointer "refs/heads/master" "a.txt" "$txt_oid" "120"
148
149   assert_local_object "$nested_md_oid" 140
150   assert_local_object "$txt_oid" 120
151   refute_local_object "$md_oid" 140
152
153   # Failure should occur when trying to import a.md as no entry exists in
154   # top-level .gitattributes file
155   git lfs migrate import --no-rewrite --yes a.md 2>&1 | tee migrate.log
156   if [ ${PIPESTATUS[0]} -eq 0 ]; then
157     echo >&2 "fatal: expected git lfs migrate import --no-rewrite to fail, didn't"
158     exit 1
159   fi
160
161   grep "a.md did not match any Git LFS filters in .gitattributes" migrate.log
162 )
163 end_test
164
165 begin_test "migrate import --no-rewrite (with commit message)"
166 (
167   set -e
168
169   setup_local_branch_with_gitattrs
170
171   prev_commit_oid="$(git rev-parse HEAD)"
172   expected_commit_msg="run git-lfs migrate import --no-rewrite"
173
174   git lfs migrate import --message "$expected_commit_msg" --no-rewrite --yes *.txt
175
176   # Ensure the git history remained the same
177   new_commit_oid="$(git rev-parse HEAD~1)"
178   if [ "$prev_commit_oid" != "$new_commit_oid" ]; then
179     exit 1
180   fi
181
182   # Ensure a new commit was made
183   new_head_oid="$(git rev-parse HEAD)"
184   if [ "$prev_commit_oid" == "$new_oid" ]; then
185     exit 1
186   fi
187
188   # Ensure the provided commit message was used
189   commit_msg="$(git log -1 --pretty=format:%s)"
190   if [ "$commit_msg" != "$expected_commit_msg" ]; then
191     exit 1
192   fi
193 )
194 end_test
195
196 begin_test "migrate import --no-rewrite (with empty commit message)"
197 (
198   set -e
199
200   setup_local_branch_with_gitattrs
201
202   prev_commit_oid="$(git rev-parse HEAD)"
203
204   git lfs migrate import -m "" --no-rewrite --yes *.txt
205
206   # Ensure the git history remained the same
207   new_commit_oid="$(git rev-parse HEAD~1)"
208   if [ "$prev_commit_oid" != "$new_commit_oid" ]; then
209     exit 1
210   fi
211
212   # Ensure a new commit was made
213   new_head_oid="$(git rev-parse HEAD)"
214   if [ "$prev_commit_oid" == "$new_oid" ]; then
215     exit 1
216   fi
217
218   # Ensure the provided commit message was used
219   commit_msg="$(git log -1 --pretty=format:%s)"
220   if [ "$commit_msg" != "" ]; then
221     exit 1
222   fi
223 )
224 end_test