Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 564
Expand file tree
/
Copy pathcommit.js
More file actions
Latest commit
86 lines (75 loc) · 2.16 KB
/
Copy pathcommit.js
File metadata and controls
86 lines (75 loc) · 2.16 KB
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
import{execSync,spawn}from'child_process';
importpathfrom'path';
import{writeFileSync,openSync,closeSync}from'fs';
importdedentfrom'dedent';
export{commit};
/**
* Asynchronously git commit at a given path with a message
*/
functioncommit(repoPath,message,options,done){
letcalled=false;
// commit the file by spawning a git process, unless the --hook
// option was provided. in that case, write the commit message into
// the .git/COMMIT_EDITMSG file
if(!options.hookMode){
letargs=['commit','-m',dedent(message), ...(options.args||[])];
letchild=spawn('git',args,{
cwd: repoPath,
stdio: options.quiet ? 'ignore' : 'inherit'
});
child.on('error',function(err){
if(called)return;
called=true;
done(err);
});
child.on('exit',function(code,signal){
if(called)return;
called=true;
if(code){
if(code===128){
console.warn(`
Git exited with code 128. Did you forget to run:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
`)
}
done(Object.assign(newError(`git exited with error code ${code}`),{ code, signal }));
}else{
done(null);
}
});
}else{
constgitDirPath=execSync(
'git rev-parse --absolute-git-dir',
{cwd: repoPath,encoding: 'utf8'},
).trim();
constcommitFilePath=path.join(gitDirPath,'COMMIT_EDITMSG');
try{
constfd=openSync(commitFilePath,'w');
try{
writeFileSync(fd,dedent(message));
done(null);
}catch(e){
done(e);
}finally{
closeSync(fd);
}
}catch(e){
// windows doesn't allow opening existing hidden files
// in 'w' mode... but it does let you do 'r+'!
try{
constfd=openSync(commitFilePath,'r+');
try{
writeFileSync(fd,dedent(message));
done(null);
}catch(e){
done(e);
}finally{
closeSync(fd);
}
}catch(e){
done(e);
}
}
}
}