FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
gitlet/gitlet/gitlet/Commit.java at master · xUser5000/gitlet · GitHub
xUser5000
/
gitlet
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
6
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
gitlet
/
gitlet
/
gitlet
/
Commit.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
155 lines (129 loc) · 4.46 KB
Breadcrumbs
gitlet
/
gitlet
/
gitlet
/
Commit.java
Copy path
File metadata and controls
155 lines (129 loc) · 4.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
gitlet
;
import
java
.
io
.
File
;
import
java
.
io
.
Serializable
;
import
java
.
util
.*;
import
static
gitlet
.
Utils
.*;
/** Represents a gitlet commit object.
* @author xUser5000
*/
public
class
Commit
implements
Serializable
,
Dumpable
,
Comparable
<
Commit
> {
private
final
String
message
;
private
final
Date
timestamp
;
/** Original parent from the */
private
final
String
parent
;
private
final
String
secondaryParent
;
private
final
Map
<
String
,
String
>
trackedFiles
;
private
final
String
hash
;
private
Commit
(
String
message
,
Date
timestamp
,
String
parent
,
String
secondaryParent
,
Map
<
String
,
String
>
trackedFiles
) {
this
.
message
=
message
;
this
.
timestamp
= (
timestamp
!=
null
) ?
timestamp
:
new
Date
();
this
.
parent
=
parent
;
this
.
secondaryParent
=
secondaryParent
;
this
.
trackedFiles
= (
trackedFiles
!=
null
) ?
trackedFiles
:
new
TreeMap
<>();
this
.
hash
=
generateHash
();
}
@
Override
public
int
compareTo
(
Commit
other
) {
return
this
.
getHash
().
compareTo
(
other
.
getHash
());
}
/* Builder class */
public
static
class
Builder
{
private
String
message
;
private
Date
timestamp
;
private
String
parent
;
private
String
secondaryParent
;
private
Map
<
String
,
String
>
trackedFiles
;
public
Builder
(
String
message
) {
this
.
message
=
message
;
}
public
Builder
timestamp
(
Date
timestamp
) {
this
.
timestamp
=
timestamp
;
return
this
;
}
public
Builder
parent
(
String
parent
) {
this
.
parent
=
parent
;
return
this
;
}
public
Builder
secondaryParent
(
String
secondaryParent
) {
this
.
secondaryParent
=
secondaryParent
;
return
this
;
}
public
Builder
trackedFiles
(
Map
<
String
,
String
>
trackedFiles
) {
this
.
trackedFiles
=
trackedFiles
;
return
this
;
}
public
Commit
build
() {
return
new
Commit
(
message
,
timestamp
,
parent
,
secondaryParent
,
trackedFiles
);
}
}
private
String
generateHash
() {
List
<
Object
>
hashItems
=
new
ArrayList
<>();
hashItems
.
add
(
message
);
hashItems
.
add
(
timestamp
.
toString
());
hashItems
.
add
((
parent
==
null
) ?
""
:
parent
);
hashItems
.
add
((
secondaryParent
==
null
) ?
""
:
secondaryParent
);
for
(
Map
.
Entry
<
String
,
String
>
entry
:
trackedFiles
.
entrySet
()) {
hashItems
.
add
(
entry
.
toString
());
}
return
sha1
(
hashItems
);
}
public
Date
getTimestamp
() {
return
timestamp
;
}
public
String
getMessage
() {
return
message
;
}
public
String
getParent
() {
return
parent
;
}
public
String
getSecondaryParent
() {
return
secondaryParent
;
}
public
Map
<
String
,
String
>
getTrackedFiles
() {
return
trackedFiles
;
}
public
String
getHash
() {
return
hash
;
}
public
void
saveCommit
(
File
commitsDirectory
) {
File
commitFile
=
join
(
commitsDirectory
,
hash
);
writeObject
(
commitFile
,
this
);
}
public
String
log
() {
StringBuilder
builder
=
new
StringBuilder
();
builder
.
append
(
"===
\n
"
);
builder
.
append
(
String
.
format
(
"commit %s
\n
"
,
getHash
()));
if
(
getParent
() !=
null
&&
getSecondaryParent
() !=
null
) {
builder
.
append
(
String
.
format
(
"Merge: %s %s
\n
"
,
getParent
().
substring
(
0
,
7
),
getSecondaryParent
().
substring
(
0
,
7
))
);
}
Formatter
formatter
=
new
Formatter
().
format
(
"Date: %1$ta %1$tb %1$td %1$tT %1$tY %1$tz"
,
getTimestamp
());
String
formattedDate
=
formatter
.
toString
();
builder
.
append
(
String
.
format
(
"%s
\n
"
,
formattedDate
));
builder
.
append
(
String
.
format
(
"%s
\n
\n
"
,
getMessage
()));
return
builder
.
toString
();
}
@
Override
public
boolean
equals
(
Object
obj
) {
if
(
obj
instanceof
Commit
) {
return
((
Commit
)
obj
).
getHash
().
equals
(
this
.
getHash
());
}
return
false
;
}
@
Override
public
String
toString
() {
return
"Commit{"
+
"timestamp="
+
timestamp
+
", message='"
+
message
+
'\''
+
", parent='"
+
parent
+
'\''
+
", secondaryParent='"
+
secondaryParent
+
'\''
+
", trackedFiles="
+
trackedFiles
+
", hash='"
+
hash
+
'\''
+
'}'
;
}
@
Override
public
void
dump
() {
System
.
out
.
println
(
this
);
}
}
Back
|
FazBrowse Home
|
New Git URL