-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRemote.java
284 lines (247 loc) · 13.6 KB
/
Remote.java
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
import java.util.ArrayList;
import java.util.HashMap;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.security.NoSuchAlgorithmException;
public class Remote {
private static void init(String src) throws IOException, NoSuchAlgorithmException {
File gitlet = new File(src);
File commits = new File(src + "/commits");
File branches = new File(src + "/branches");
File trees = new File(src + "/trees");
File files = new File(src + "/files");
File remotes = new File(src + "/remotes");
gitlet.mkdir();
commits.mkdir();
branches.mkdir();
trees.mkdir();
files.mkdir();
remotes.mkdir();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(commits.toString() + "/" + FileHelper.initialCommit, false)));
out.println("");
out.println("");
out.println("initial commit");
out.close();
out = new PrintWriter(new BufferedWriter(new FileWriter(branches.toString() + "/master.branch")));
out.println(FileHelper.initialCommit);
out.close();
}
public static void addRemote (String remoteName, String userName, String serverName, String src) throws IOException {
File remoteFile = new File(State.srcDir + "/.gitlet/remotes/" + remoteName + ".remote");
if (remoteFile.exists()) {
System.out.println("A remote with that name already exists.");
} else {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(remoteFile.toString())));
out.println(userName);
out.println(serverName);
out.println(src);
out.close();
}
}
public static void rmRemote (String remoteName) throws IOException {
File remoteFile = new File(State.srcDir + "/.gitlet/remotes/" + remoteName + ".remote");
if (remoteFile.exists()) {
remoteFile.delete();
} else {
System.out.println("A remote with that name does not exist.");
}
}
public static void push (String remoteName, String remoteBranchName) throws FileNotFoundException, IOException, NoSuchAlgorithmException {
File remoteFile = new File(State.srcDir + "/.gitlet/remotes/" + remoteName + ".remote");
if (remoteFile.exists()) {
BufferedReader in = new BufferedReader(new FileReader(remoteFile.toString()));
String userName = in.readLine();
String serverName = in.readLine();
String src = in.readLine();
in.close();
if (execute("ssh " + userName + "@" + serverName + " 'ls " + src + "'", null) == null) {
execute("ssh " + userName + "@" + serverName + " 'mkdir -p " + src + "'", null);
}
String gitlet = src + "/.gitlet";
if (execute("ssh " + userName + "@" + serverName + " 'ls " + gitlet + "'", null) == null) {
execute("scp -r './.gitlet' " + userName + "@" + serverName + ":" + gitlet, null);
} else {
String branch = gitlet + "/branches/" + remoteBranchName + ".branch";
if (execute("ssh " + userName + "@" + serverName + " 'ls " + branch + "'", null) == null) {
String tempFile = new File("_x.tmp").toString();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempFile, false)));
out.println(FileHelper.initialCommit);
out.close();
execute("scp " + tempFile + " " + userName + "@" + serverName + ":" + branch, null);
new File(tempFile).delete();
}
BufferedReader br = execute("ssh " + userName + "@" + serverName + " 'cat " + branch + "'", null);
String remoteCommit = br.readLine();
br.close();
ArrayList<String> localCommits = FileHelper.getCommitTree(FileHelper.getCommit(State.head), FileHelper.initialCommit);
if (localCommits.contains(remoteCommit)) {
ArrayList<String> commits = FileHelper.getCommitTree(FileHelper.getCommit(State.head), remoteCommit);
for (String c : commits) {
execute("scp ./.gitlet/commits/" + c + " " + userName + "@" + serverName + ":" + gitlet + "/commits/" + c, null);
if (!c.equals(FileHelper.initialCommit)) {
String tree = FileHelper.getTree(c);
execute("scp ./.gitlet/trees/" + tree + " " + userName + "@" + serverName + ":" + gitlet + "/trees/" + tree, null);
HashMap<String, String> files = FileHelper.listFilesInCommit(c);
for (String fileHash : files.values()) {
execute("scp ./.gitlet/files/" + fileHash + " " + userName + "@" + serverName + ":" + gitlet + "/files/" + fileHash, null);
}
}
}
String tempFile = new File("_x.tmp").toString();
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(tempFile, false)));
out.println(FileHelper.getCommit(State.head));
out.close();
execute("scp " + tempFile + " " + userName + "@" + serverName + ":" + branch, null);
new File(tempFile).delete();
} else {
System.out.println("Please pull down remote changes before pushing.");
}
}
}
}
public static void pull(String remoteName, String remoteBranchName) throws IOException, FileNotFoundException, NoSuchAlgorithmException {
File remoteFile = new File(State.srcDir + "/.gitlet/remotes/" + remoteName + ".remote");
if (remoteFile.exists()) {
BufferedReader in = new BufferedReader(new FileReader(remoteFile.toString()));
String userName = in.readLine();
String serverName = in.readLine();
String src = in.readLine();
in.close();
String gitlet = src + "/.gitlet";
String branch = gitlet + "/branches/" + remoteBranchName + ".branch";
BufferedReader br = execute("ssh " + userName + "@" + serverName + " 'cat " + branch + "'", null);
if (br == null) {
System.out.println("That remote does not have that branch.");
} else {
String remoteCommit = br.readLine();
br.close();
String localCommit = FileHelper.getCommit(State.head);
ArrayList<String> localCommits = FileHelper.getCommitTree(localCommit, FileHelper.initialCommit);
if (localCommits.contains(remoteCommit)) {
System.out.println("Already up-to-date.");
return;
}
ArrayList<String> remoteCommits = getRemoteCommitTree(userName, serverName, src, remoteCommit, FileHelper.initialCommit);
if (remoteCommits.contains(localCommit)) {
ArrayList<String> commits = getRemoteCommitTree(userName, serverName, src, remoteCommit, localCommit);
for (int i = commits.size() - 2; i >= 0; i--) {
String commit = commits.get(i);
execute("scp " + userName + "@" + serverName + ":" + gitlet + "/commits/" + commit + " " + "./.gitlet/commits/" + commit, null);
if (!commit.equals(FileHelper.initialCommit)) {
String tree = FileHelper.getTree(commit);
execute("scp " + userName + "@" + serverName + ":" + gitlet + "/trees/" + tree + " " + "./.gitlet/trees/" + tree, null);
HashMap<String, String> files = FileHelper.listFilesInCommit(commit);
for (String fileHash : files.values()) {
execute("scp " + userName + "@" + serverName + ":" + gitlet + "/files/" + fileHash + " " + "./.gitlet/files/" + fileHash, null);
}
}
if (i == 0) {
Commands.reset(commit.substring(0, 64));
//FileHelper.updateBranch(State.head, commit);
return;
}
}
} else {
remoteCommits.retainAll(localCommits);
String commonAncestor = remoteCommits.get(0);
ArrayList<String> commits = getRemoteCommitTree(userName, serverName, src, remoteCommit, commonAncestor);
for (int i = commits.size() - 2; i >= 0; i--) {
String commit = commits.get(i);
execute("scp " + userName + "@" + serverName + ":" + gitlet + "/commits/" + commit + " " + "./.gitlet/commits/" + commit, null);
String tree = FileHelper.getTree(commit);
execute("scp " + userName + "@" + serverName + ":" + gitlet + "/trees/" + tree + " " + "./.gitlet/trees/" + tree, null);
HashMap<String, String> files = FileHelper.listFilesInCommit(commit);
for (String fileHash : files.values()) {
execute("scp " + userName + "@" + serverName + ":" + gitlet + "/files/" + fileHash + " " + "./.gitlet/files/" + fileHash, null);
}
}
HashMap<String, String> commonAncestorFiles = FileHelper.listFilesInCommit(commonAncestor);
HashMap<String, String> localCommitFiles = FileHelper.listFilesInCommit(localCommit);
HashMap<String, String> modifiedFiles = new HashMap<String, String>();
for (String fileName : localCommitFiles.keySet()) {
String ancestorFileHash = commonAncestorFiles.get(fileName);
String localFileHash = localCommitFiles.get(fileName);
if (ancestorFileHash == null || !ancestorFileHash.equals(localFileHash)) {
modifiedFiles.put(fileName, localFileHash);
}
}
if (!modifiedFiles.isEmpty())
{
HashMap<String, String> CommitFiles = FileHelper.listFilesInCommit(remoteCommit);
for (String fileName : modifiedFiles.keySet()) {
CommitFiles.put(fileName, localCommitFiles.get(fileName));
}
String treeName = FileHelper.createTree(CommitFiles);
String commit = FileHelper.commitFromTree(remoteCommit, treeName, FileHelper.getCommitMessage(localCommit) + " - merged");
Commands.reset(commit.substring(0, 64));
} else {
Commands.reset(remoteCommit.substring(0, 64));
}
}
}
}
}
public static void clone(String remoteName) throws IOException, FileNotFoundException, ClassNotFoundException {
File remoteFile = new File(State.srcDir + "/.gitlet/remotes/" + remoteName + ".remote");
if (remoteFile.exists()) {
BufferedReader in = new BufferedReader(new FileReader(remoteFile.toString()));
String userName = in.readLine();
String serverName = in.readLine();
String src = in.readLine();
in.close();
String gitlet = src + "/.gitlet";
File localDir = new File("./" + remoteName);
if (!localDir.exists()) {
localDir.mkdirs();
}
execute("scp -r " + userName + "@" + serverName + ":" + gitlet + " ./" + remoteName + "/.gitlet", null);
System.setProperty("user.dir", new File(remoteName).getAbsolutePath());
State.load();
String commit = FileHelper.getCommit(State.head);
Commands.reset(commit.substring(0, 64));
} else {
System.out.println("A remote with that name does not exist.");
}
}
private static BufferedReader execute (String command, File workingDirectory) throws IOException {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command, null, workingDirectory);
String s;
boolean error = false;
BufferedReader br = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
while ((s = br.readLine()) != null) {
error = true;
}
br.close();
if (error) {
return null;
} else {
return new BufferedReader(new InputStreamReader(proc.getInputStream()));
}
}
public static ArrayList<String> getRemoteCommitTree(String userName, String serverName, String src, String startCommit, String endCommit) throws IOException {
ArrayList<String> commitTree = new ArrayList<String>();
String prevCommit = startCommit;
String gitletCommit = src + "/.gitlet/commits/";
while (true) {
commitTree.add(prevCommit);
if (prevCommit.equals(endCommit)) {
break;
}
BufferedReader br = execute("ssh " + userName + "@" + serverName + " 'cat " + gitletCommit + prevCommit + "'", null);
prevCommit = br.readLine();
br.close();
}
return commitTree;
}
}