1 # git-gui misc. commit reading/writing support
2 # Copyright (C) 2006, 2007 Shawn Pearce
4 proc load_last_commit
{} {
5 global HEAD PARENT MERGE_HEAD commit_type ui_comm
8 if {[llength $PARENT] == 0} {
9 error_popup
{There is nothing to amend.
11 You are about to create the initial commit. There is no commit before this to amend.
16 repository_state curType curHEAD curMERGE_HEAD
17 if {$curType eq
{merge
}} {
18 error_popup
{Cannot amend
while merging.
20 You are currently in the middle of a merge that has not been fully completed. You cannot amend the prior commit unless you first abort the current merge activity.
28 set fd
[open "| git cat-file commit $curHEAD" r
]
29 fconfigure $fd -encoding binary -translation lf
30 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
33 while {[gets $fd line
] > 0} {
34 if {[string match
{parent
*} $line]} {
35 lappend parents
[string range
$line 7 end
]
36 } elseif
{[string match
{encoding *} $line]} {
37 set enc
[string tolower
[string range
$line 9 end
]]
40 set msg
[encoding convertfrom
$enc [read $fd]]
41 set msg
[string trim
$msg]
44 error_popup
"Error loading commit data for amend:\n\n$err"
51 switch -- [llength $parents] {
52 0 {set commit_type amend-initial
}
53 1 {set commit_type amend
}
54 default {set commit_type amend-merge
}
57 $ui_comm delete
0.0 end
58 $ui_comm insert end
$msg
60 $ui_comm edit modified false
61 rescan
{set ui_status_value
{Ready.
}}
64 set GIT_COMMITTER_IDENT
{}
66 proc committer_ident
{} {
67 global GIT_COMMITTER_IDENT
69 if {$GIT_COMMITTER_IDENT eq
{}} {
70 if {[catch {set me
[git var GIT_COMMITTER_IDENT
]} err
]} {
71 error_popup
"Unable to obtain your identity:\n\n$err"
74 if {![regexp {^
(.
*) [0-9]+ [-+0-9]+$} \
75 $me me GIT_COMMITTER_IDENT
]} {
76 error_popup
"Invalid GIT_COMMITTER_IDENT:\n\n$me"
81 return $GIT_COMMITTER_IDENT
87 set me
[committer_ident
]
90 set sob
"Signed-off-by: $me"
91 set last
[$ui_comm get
{end
-1c linestart
} {end
-1c}]
93 $ui_comm edit separator
95 && ![regexp {^
[A-Z
][A-Za-z
]*-[A-Za-z-
]+: *} $last]} {
96 $ui_comm insert end
"\n"
98 $ui_comm insert end
"\n$sob"
99 $ui_comm edit separator
104 proc create_new_commit
{} {
105 global commit_type ui_comm
107 set commit_type normal
108 $ui_comm delete
0.0 end
110 $ui_comm edit modified false
111 rescan
{set ui_status_value
{Ready.
}}
114 proc commit_tree
{} {
115 global HEAD commit_type file_states ui_comm repo_config
116 global ui_status_value pch_error
118 if {[committer_ident
] eq
{}} return
119 if {![lock_index
update]} return
121 # -- Our in memory state should match the repository.
123 repository_state curType curHEAD curMERGE_HEAD
124 if {[string match amend
* $commit_type]
125 && $curType eq
{normal
}
126 && $curHEAD eq
$HEAD} {
127 } elseif
{$commit_type ne
$curType ||
$HEAD ne
$curHEAD} {
128 info_popup
{Last scanned state does not match repository state.
130 Another Git program has modified this repository since the last
scan. A rescan must be performed before another commit can be created.
132 The rescan will be automatically started now.
135 rescan
{set ui_status_value
{Ready.
}}
139 # -- At least one file should differ in the index.
142 foreach path
[array names file_states
] {
143 switch -glob -- [lindex $file_states($path) 0] {
147 M?
{set files_ready
1}
149 error_popup
"Unmerged files cannot be committed.
151 File [short_path $path] has merge conflicts. You must resolve them and add the file before committing.
157 error_popup
"Unknown file state [lindex $s 0] detected.
159 File [short_path $path] cannot be committed by this program.
164 if {!$files_ready && ![string match
*merge
$curType]} {
165 info_popup
{No changes to commit.
167 You must add at least
1 file before you can commit.
173 # -- A message is required.
175 set msg
[string trim
[$ui_comm get
1.0 end
]]
176 regsub -all -line {[ \t\r]+$} $msg {} msg
178 error_popup
{Please supply a commit
message.
180 A good commit
message has the following
format:
182 - First line
: Describe in one sentance what you did.
184 - Remaining lines
: Describe why this change is good.
190 # -- Run the pre-commit hook.
192 set pchook
[gitdir hooks pre-commit
]
194 # On Cygwin [file executable] might lie so we need to ask
195 # the shell if the hook is executable. Yes that's annoying.
197 if {[is_Cygwin
] && [file isfile
$pchook]} {
198 set pchook
[list sh
-c [concat \
199 "if test -x \"$pchook\";" \
200 "then exec \"$pchook\" 2>&1;" \
202 } elseif
{[file executable
$pchook]} {
203 set pchook
[list $pchook |
& cat
]
205 commit_writetree
$curHEAD $msg
209 set ui_status_value
{Calling pre-commit hook...
}
211 set fd_ph
[open "| $pchook" r
]
212 fconfigure $fd_ph -blocking 0 -translation binary
213 fileevent $fd_ph readable
\
214 [list commit_prehook_wait
$fd_ph $curHEAD $msg]
217 proc commit_prehook_wait
{fd_ph curHEAD msg
} {
218 global pch_error ui_status_value
220 append pch_error
[read $fd_ph]
221 fconfigure $fd_ph -blocking 1
223 if {[catch {close $fd_ph}]} {
224 set ui_status_value
{Commit declined by pre-commit hook.
}
225 hook_failed_popup pre-commit
$pch_error
228 commit_writetree
$curHEAD $msg
233 fconfigure $fd_ph -blocking 0
236 proc commit_writetree
{curHEAD msg
} {
237 global ui_status_value
239 set ui_status_value
{Committing changes...
}
240 set fd_wt
[open "| git write-tree" r
]
241 fileevent $fd_wt readable
\
242 [list commit_committree
$fd_wt $curHEAD $msg]
245 proc commit_committree
{fd_wt curHEAD msg
} {
246 global HEAD PARENT MERGE_HEAD commit_type
247 global all_heads current_branch
248 global ui_status_value ui_comm selected_commit_type
249 global file_states selected_paths rescan_active
253 if {$tree_id eq
{} ||
[catch {close $fd_wt} err
]} {
254 error_popup
"write-tree failed:\n\n$err"
255 set ui_status_value
{Commit failed.
}
260 # -- Verify this wasn't an empty change.
262 if {$commit_type eq
{normal
}} {
263 set fd_ot
[open "| git cat-file commit $PARENT" r
]
264 fconfigure $fd_ot -encoding binary -translation lf
265 set old_tree
[gets $fd_ot]
268 if {[string equal
-length 5 {tree
} $old_tree]
269 && [string length
$old_tree] == 45} {
270 set old_tree
[string range
$old_tree 5 end
]
272 error "Commit $PARENT appears to be corrupt"
275 if {$tree_id eq
$old_tree} {
276 info_popup
{No changes to commit.
278 No files were modified by this commit and it was not a merge commit.
280 A rescan will be automatically started now.
283 rescan
{set ui_status_value
{No changes to commit.
}}
288 # -- Build the message.
290 set msg_p
[gitdir COMMIT_EDITMSG
]
291 set msg_wt
[open $msg_p w
]
292 if {[catch {set enc
$repo_config(i18n.commitencoding
)}]} {
295 fconfigure $msg_wt -encoding binary -translation binary
296 puts -nonewline $msg_wt [encoding convertto
$enc $msg]
299 # -- Create the commit.
301 set cmd
[list commit-tree
$tree_id]
302 foreach p
[concat $PARENT $MERGE_HEAD] {
306 if {[catch {set cmt_id
[eval git
$cmd]} err
]} {
307 error_popup
"commit-tree failed:\n\n$err"
308 set ui_status_value
{Commit failed.
}
313 # -- Update the HEAD ref.
316 if {$commit_type ne
{normal
}} {
317 append reflogm
" ($commit_type)"
319 set i
[string first
"\n" $msg]
321 set subject
[string range
$msg 0 [expr {$i - 1}]]
325 append reflogm
{: } $subject
327 git update-ref
-m $reflogm HEAD
$cmt_id $curHEAD
329 error_popup
"update-ref failed:\n\n$err"
330 set ui_status_value
{Commit failed.
}
335 # -- Cleanup after ourselves.
337 catch {file delete
$msg_p}
338 catch {file delete
[gitdir MERGE_HEAD
]}
339 catch {file delete
[gitdir MERGE_MSG
]}
340 catch {file delete
[gitdir SQUASH_MSG
]}
341 catch {file delete
[gitdir GITGUI_MSG
]}
343 # -- Let rerere do its thing.
345 if {[file isdirectory
[gitdir rr-cache
]]} {
349 # -- Run the post-commit hook.
351 set pchook
[gitdir hooks post-commit
]
352 if {[is_Cygwin
] && [file isfile
$pchook]} {
353 set pchook
[list sh
-c [concat \
354 "if test -x \"$pchook\";" \
355 "then exec \"$pchook\";" \
357 } elseif
{![file executable
$pchook]} {
361 catch {exec $pchook &}
364 $ui_comm delete
0.0 end
366 $ui_comm edit modified false
368 if {[is_enabled singlecommit
]} do_quit
370 # -- Make sure our current branch exists.
372 if {$commit_type eq
{initial
}} {
373 lappend all_heads
$current_branch
374 set all_heads
[lsort -unique $all_heads]
378 # -- Update in memory status
380 set selected_commit_type new
381 set commit_type normal
384 set MERGE_HEAD
[list]
386 foreach path
[array names file_states
] {
387 set s
$file_states($path)
397 unset file_states
($path)
398 catch {unset selected_paths
($path)}
401 set file_states
($path) [list _O
[lindex $s 1] {} {}]
407 set file_states
($path) [list \
408 _
[string index
$m 1] \
419 set ui_status_value
\
420 "Created commit [string range $cmt_id 0 7]: $subject"