Calling git.init() when repository already exists - jgit

I am using the following snippet of code to initialize the git repository:
try (Git git = Git.init().setDirectory(gitFilename).call()) {
log.info("Created repository: " + git.getRepository().getDirectory());
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
What happens when the repository is already created and I call git.init() on it?
Thanks! Ana

This appeared to work for me:
//initialize git repository
String gitDirectory = getGitRepoDirectory();
File gitFilename = new File(gitDirectory);
try (Git git = Git.open(gitFilename)){
log.info("Git repo " + gitDirectory + " exists!");;
} catch (RepositoryNotFoundException e) {
log.info("Initialising " + gitDirectory + " as a git repo for backup purposes");
try {
Git git = Git.init().setDirectory(gitFilename).call();
} catch (GitAPIException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Related

How to Write/Read in JavaFX with Gluon Plugin

I am trying to write/read files in JavaFX while using the Gluon plugin, and I am not having any luck. If i run the program as a Desktop App, it works fine, I've even used the same functions in Android IDE and it works fine, however if I send the program to my phone with Gluon, it does not work. I wanting to know how to save/read to files in Gluon, and what have I done wrong, and why does it work as Desktop and on Android but not Gluon. The two fcns I used are:
public void writeToFile() {
try {
FileOutputStream f = new FileOutputStream("Meal1_Protein.txt");
f.write(choice.getBytes()); //Choice is input from a dropdown
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast toast = new Toast(choice);
toast.show();
}
public void readFromFile(){
try {
FileInputStream f = new FileInputStream("Meal1_Protein.txt");
InputStreamReader is = new InputStreamReader(f);
BufferedReader br = new BufferedReader(is);
StringBuffer buff = new StringBuffer();
String lines;
while((lines = br.readLine())!=null){
buff.append(lines);
buff.append("\n");
}
carbInput.setText(buff.toString()); // inserting into a txtfield
//to see if it I can read strings back in.
Toast toast = new Toast(buff.toString());
toast.show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

getServletContext().getResource(file3) returns null url resource

Here is my code which reads excelsheet files.but i can see my other functions are working fine but when try to get resource as url it return null.
URL resource = null;
try {
System.out.println(file3);
resource = getServletContext().getResource(file3);
System.out.println("Problem with getting resource"+resource);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = null;
try {
System.out.println(resource.toURI());
file = new File(resource.toURI());
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileInputStream input = null;
try {
input = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Workbook w;
w = Workbook.getWorkbook(input);
// Get the first sheet
Sheet sheet = w.getSheet(0);
return sheet;

Saving Data to a txt file with a runnable jar

Hello,
I can't find out how to make it so i can save data to a txt. It works while I'm in eclipse but not when i export as a runnable Jar File.
My code is:
public void openFile()
File file = new File(System.getProperty("user.dir"), "src/save/Kube.txt");
FileWriter fw = null;
try {
fw = new FileWriter(file.getAbsoluteFile());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x = new BufferedWriter(fw);
}
public void addRecords() {
try {
x.write(game.getTotalCoins() + "\n");
x.write(game.getHighScore() + "\n");
x.write(game.getBackround() + "\n");
x.write(game.getCube() + "\n");
x.write(game.isBlackselected() + "\n");
x.write(game.isBlueselected() + "\n");
x.write(game.isGreenselected() + "\n");
x.write(game.isBlueunlocked() + "\n");
x.write(game.isGreenunlocked() + "\n");
x.write(game.isDefaultselected() + "\n");
x.write(game.isSkyselected() + "\n");
x.write(game.isUnderwaterselected() + "\n");
x.write(game.isSkyunlocked() + "\n");
x.write(game.isUnderwaterunlocked() + "");
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeFile() {
try {
x.close();
} catch (IOException e) {
e.printStackTrace();
}
}
When ran in eclipse it works fine but when made into runnable jar file does not save.
Thanks for your help!

jgit pull issue HEAD DETACHED

I am trying to use jgit to pull the changes from a git repository.
I am facing minor issues with HEAD DETACHED error when I do a pull
I have read other answers here on stackoverflow. Trying those solutions does not seem to help.
I have 2 remote branches
1. staging
2. master
Here is the sample code:
public static void main(String [] args){
final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git";
final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar;
try{
//setting up local repository as just testNewLiferRepo
String repoName = localRepositoryPath + "testNewLiferRepo";
File f = new File(repoName);
Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git");
ObjectId oldObjid = null;
Git git = null;
if (f.exists()) {
oldObjid = localRepo.resolve(Constants.HEAD);
git = Git.open(f);
git.pull().call();
ObjectId currentObjid = localRepo.resolve(Constants.HEAD);
git.getRepository().close();
} else {
git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call();
ObjectId currentObjid =localRepo.resolve(Constants.HEAD);
if(!localRepo.getBranch().equalsIgnoreCase("staging")){
git.checkout().setName("refs/remotes/origin/staging").setForce(true).call();
}
git.getRepository().close();
}
} catch (InvalidRemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RevisionSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AmbiguousObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IncorrectObjectTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You can update the version in the testXML file on the repository to test the pull.
Any pointers are highly appreciated
Thanks
Looks like I had my statements switched. Here is a new solution
public static void main(String [] args){
final String GIT_STR = "https://github.com/newlifer2/testNewLiferRepo.git";
final String localRepositoryPath = new File("").getAbsolutePath() + File.separatorChar;
SSHSessionFactory sessionFactory = new SSHSessionFactory();
sessionFactory.Initialize();
SshSessionFactory.setInstance(sessionFactory);
try{
//setting up local repository as just testNewLiferRepo
String repoName = localRepositoryPath + "testNewLiferRepo";
File f = new File(repoName);
Repository localRepo = new FileRepository(repoName + File.separatorChar + ".git");
ObjectId oldObjid = null;
Git git = null;
if (f.exists()) {
oldObjid = localRepo.resolve(Constants.HEAD);
git = Git.open(f);
Map<String,Ref> m = git.getRepository().getAllRefs();
System.out.println("Current Branch: "+git.getRepository().getBranch());
if(git.getRepository().isValidRefName("refs/remotes/origin/staging")){
System.out.println("Valid");
}
git.pull().call();
git.checkout().setName("staging").call();
ObjectId currentObjid = localRepo.resolve(Constants.HEAD);
git.getRepository().close();
} else {
git = Git.cloneRepository().setURI(GIT_STR).setDirectory(new File(repoName)).call();
ObjectId currentObjid =localRepo.resolve(Constants.HEAD);
if(!localRepo.getBranch().equalsIgnoreCase("staging")){
git.branchCreate().setForce(true).setName("staging").setStartPoint("refs/remotes/origin/staging").call();
}
git.checkout().setName("staging").call();
git.getRepository().close();
}
} catch (InvalidRemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RevisionSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AmbiguousObjectException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IncorrectObjectTypeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

IOException: The process cannot access the file 'filename' because it is being used by another process

I have this code in my asp.net project:
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
using (StreamWriter sw = File.CreateText(Server.MapPath(#"~/AboutUs.txt")))
{
sw.Write(FreeTextBox1.Text);
sw.Close();
sw.Dispose();
lblError.Text = "تغییرات با موفقیت ذخیره شد.";
lblError.CssClass = "success";
}
}
catch (Exception ex)
{
lblError.Text = "خطایی روی داده است!" + "\n" + ex.ToString();
lblError.CssClass = "error";
}
finally
{
lblError.Visible = true;
}
}
Sometimes (not always) when I hit btnSave following error is occurred:
IOException: The process cannot access the file 'filename' because it is being used by another process
Why?
You just Run your application after see windows task manager-->Processes, any notepad.exe running you just right click then click end process . .

Resources