@@ -105,17 +105,32 @@ public static String getBranchPath(FileIO fileIO, Path tablePath, String branchN
105
105
return tablePath .toString () + "/branch/" + BRANCH_PREFIX + branchName ;
106
106
}
107
107
108
+ public String defaultMainBranch () {
109
+ Path path = new Path (tablePath , MAIN_BRANCH_FILE );
110
+ try {
111
+ if (fileIO .exists (path )) {
112
+ String data = fileIO .readFileUtf8 (path );
113
+ if (!StringUtils .isBlank (data )) {
114
+ return data ;
115
+ }
116
+ }
117
+ return DEFAULT_MAIN_BRANCH ;
118
+ } catch (IOException e ) {
119
+ throw new RuntimeException (e );
120
+ }
121
+ }
122
+
108
123
/** Return the path of a branch. */
109
124
public Path branchPath (String branchName ) {
110
125
return new Path (getBranchPath (fileIO , tablePath , branchName ));
111
126
}
112
127
113
128
public void createBranch (String branchName , String tagName ) {
129
+ String mainBranch = defaultMainBranch ();
114
130
checkArgument (
115
- !branchName .equals (DEFAULT_MAIN_BRANCH ),
131
+ !branchName .equals (mainBranch ),
116
132
String .format (
117
- "Branch name '%s' is the default branch and cannot be used." ,
118
- DEFAULT_MAIN_BRANCH ));
133
+ "Branch name '%s' is the default branch and cannot be used." , mainBranch ));
119
134
checkArgument (!StringUtils .isBlank (branchName ), "Branch name '%s' is blank." , branchName );
120
135
checkArgument (!branchExists (branchName ), "Branch name '%s' already exists." , branchName );
121
136
checkArgument (tagManager .tagExists (tagName ), "Tag name '%s' not exists." , tagName );
@@ -161,6 +176,12 @@ public void deleteBranch(String branchName) {
161
176
162
177
/** Replace specify branch to main branch. */
163
178
public void replaceBranch (String branchName ) {
179
+ String mainBranch = defaultMainBranch ();
180
+ checkArgument (
181
+ !branchName .equals (mainBranch ),
182
+ String .format (
183
+ "Branch name '%s' is the default main branch and cannot be replaced repeatedly." ,
184
+ mainBranch ));
164
185
checkArgument (!StringUtils .isBlank (branchName ), "Branch name '%s' is blank." , branchName );
165
186
checkArgument (branchExists (branchName ), "Branch name '%s' not exists." , branchName );
166
187
try {
0 commit comments