Show changes/diffs without a parent commit (first commit) with JGit - jgit

when I do a git show commit of the first commit in a repository, I see all the files along with the diffs of the files (i.e all the lines being added)
$ git show cb5d132
commit cb5d13286cf9d14782f0e10445456dfe41072f55
Author: tw2 tw2LastName <tw2>
Date: Thu Oct 23 05:15:09 2014 -0400
Initial Commit
diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..96d156e
--- /dev/null
+++ b/README.txt
## -0,0 +1 ##
+First Line in README file!
\ No newline at end of file
The jgit show doesn't seem to get similar info, How can I get similar output using JGit API? I could use DiffFormatter, but it seems to require the baseTree and commitTree, wondering how I can get the DiffFormatter to work on the first commit in the repository.
ByteArrayOutputStream os = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter( os )
RawTextComparator cmp = RawTextComparator.DEFAULT;
df.setRepository(repository);
df.setDiffComparator(cmp);
df.setDetectRenames(true);
// wondering how to use the API if we do not have baseCommit.
List<DiffEntry> diffEntries = df.scan(??baseCommitTree??, firstCommit.getTree());
for (DiffEntry diffEntry : diffEntries) {
df.format(diffEntry);
}
System.out.println(df.toString());

Use an o.e.jgit.treewalk.EmptyTreeIterator to compare the first commit against:
AbstractTreeIterator oldTreeIter = new EmptyTreeIterator();
ObjectReader reader = repository.newObjectReader();
AbstractTreeIterator newTreeIter = new CanonicalTreeParser(null, reader, firstCommit.getTree());
List<DiffEntry> diffEntries = df.scan(oldTreeIter, newTreeIter);
...

Related

JGit Java Changing the date of the last commits

I'm trying to make the following git commands using the JGit library:
git rebase -i HEAD~2
git commit --amend --reset-author --no-edit --date 2022-07-07T15:30:00.0000000+03:00
git rebase --continue
git commit --amend --reset-author --no-edit --date 2022-07-07T15:33:40.0000000+03:00
git rebase --continue
To do this, I have to switch to the branch I want and then run these commands.
This is what I'm trying to do:
File yourFile = new File("...\\.git");
FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
repositoryBuilder.setMustExist(true);
repositoryBuilder.setGitDir(yourFile);
Repository repository = repositoryBuilder.build();
Git git = new Git(repository);
git.checkout().setName("main").call();
RebaseCommand.InteractiveHandler squashHandler = new RebaseCommand.InteractiveHandler() {
#Override
public void prepareSteps(List<RebaseTodoLine> steps) {
for (RebaseTodoLine step : steps.subList(1, steps.size())) {
try {
step.setAction(RebaseTodoLine.Action.SQUASH);
step.setShortMessage("");
} catch (IllegalTodoFileModification e) {
throw new RuntimeException("Couldn't squash commit " + step.getCommit().name(), e);
}
}
}
#Override
public String modifyCommitMessage(String commit) {
return commit;
}
};
git.rebase().runInteractively(squashHandler).setUpstream("HEAD~" + 1).call();
And then I don't know what to do and I'm not sure what started out right.
I think the next step should be something like this:
git.commit().setAmend(true).<anything else>
Please tell me how to execute the above git commands with JGit. I tried to find documentation that describes everything in a very clear language, like this
git command: git rebase -i HEAD~2
JGit code: git.rebase().runInteractively(squashHandler).setUpstream("HEAD~" + 2).call();
But I couldn't find anything like that, and reading the documentation, I almost shot myself :) Thanks

nix-shell script does nothing when using script

I'm quite new to Nix and I'm trying to create a very simple shell.nix script file.
Unfortunately I need an old package: mariadb-10.4.21. After reading and searching a bit I found out that version 10.4.17 (would've been nice to have the exact version but I couldn't find it) is in channel nixos-20.09, but when I do
$ nix-shell --version
nix-shell (Nix) 2.5.1
$ cat shell.nix
let
pkgs = import <nixpkgs> {};
# git ls-remote https://github.com/nixos/nixpkgs nixos-20.09
pkgs-20_09 = import (builtins.fetchGit {
name = "nixpks-20.09";
url = "https://github.com/nixos/nixpkgs";
ref = "refs/heads/nixos-20.09";
rev = "1c1f5649bb9c1b0d98637c8c365228f57126f361";
}) {};
in
pkgs.stdenv.mkDerivation {
pname = "test";
version = "0.1.0";
buildInputs = [
pkgs-20_09.mariadb
];
}
$ nix-shell
it just waits indefinitely without doing anything. But if I do
$ nix-shell -p mariadb -I nixpkgs=https://github.com/NixOS/nixpkgs/archive/1c1f5649bb9c1b0d98637c8c365228f57126f361.tar.gz
[...]
/nix/store/yias2v8pm9pvfk79m65wdpcby4kiy91l-mariadb-server-10.4.17
[...]
copying path '/nix/store/yias2v8pm9pvfk79m65wdpcby4kiy91l-mariadb-server-10.4.17' from 'https://cache.nixos.org'...
[nix-shell:~/Playground]$ mariadb --version
mariadb Ver 15.1 Distrib 10.4.17-MariaDB, for Linux (x86_64) using readline 5.1
it works perfectly.
What am I doing wrong in the script for it to halt?
EDIT: I got a bit more info by running
$ nix-shell -vvv
[...]
did not find cache entry for '{"name":"nixpks-20.09","rev":"1c1f5649bb9c1b0d98637c8c365228f57126f361","type":"git"}'
did not find cache entry for '{"name":"nixpks-20.09","ref":"refs/heads/nixos-20.09","type":"git","url":"https://github.com/nixos/nixpkgs"}'
locking path '/home/test/.cache/nix/gitv3/17blyky0ja542rww32nj04jys1r9vnkg6gcfbj83drca9a862hwp.lock'
lock acquired on '/home/test/.cache/nix/gitv3/17blyky0ja542rww32nj04jys1r9vnkg6gcfbj83drca9a862hwp.lock.lock'
fetching Git repository 'https://github.com/nixos/nixpkgs'...
Is it me or it seems like it's trying to fetch from two different sources? As far as I understood all three url, rev and ref are needed for git-fetching, but it looks like if it's splitting them.
EDIT2: I've been trying with fetchFromGitHub
pkgs-20_09 = import (pkgs.fetchFromGitHub {
name = "nixpks-20.09";
owner = "nixos";
repo = "nixpkgs";
rev = "1c1f5649bb9c1b0d98637c8c365228f57126f361";
sha256 = "0f2nvdijyxfgl5kwyb4465pppd5vkhqxddx6v40k2s0z9jfhj0xl";
}) {};
and fetchTarball
pkgs-20_09 = import (builtins.fetchTarball "https://github.com/NixOS/nixpkgs/archive/1c1f5649bb9c1b0d98637c8c365228f57126f361.tar.gz") {};
and both work just fine. I'll use fetchFromGitHub from now on but it'd be interesting to now why fetchGit doesn't work.

How can I commit changes to GitHub from within a R script?

I am trying to automate some basic git operations from within a R script. I am using Rstudio on Windows OS. This may be helpful for example if you wished to update GitHub when a script finishes performing some automated task.
I wrote some simple functions that utilize R's shell() function and the Window's & pipe operator to send a chain of commands to the OS terminal:
# Git status.
gitstatus <- function(dir = getwd()){
cmd_list <- list(
cmd1 = tolower(substr(dir,1,2)),
cmd2 = paste("cd",dir),
cmd3 = "git status"
)
cmd <- paste(unlist(cmd_list),collapse = " & ")
shell(cmd)
}
# Git add.
gitadd <- function(dir = getwd()){
cmd_list <- list(
cmd1 = tolower(substr(dir,1,2)),
cmd2 = paste("cd",dir),
cmd3 = "git add --all"
)
cmd <- paste(unlist(cmd_list),collapse = " & ")
shell(cmd)
}
# Git commit.
gitcommit <- function(msg = "commit from Rstudio", dir = getwd()){
cmd_list <- list(
cmd1 = tolower(substr(dir,1,2)),
cmd2 = paste("cd",dir),
cmd3 = paste0("git commit -am ","'",msg,"'")
)
cmd <- paste(unlist(cmd_list),collapse = " & ")
shell(cmd)
}
# Git push.
gitpush <- function(dir = getwd()){
cmd_list <- list(
cmd1 = tolower(substr(dir,1,2)),
cmd2 = paste("cd",dir),
cmd3 = "git push"
)
cmd <- paste(unlist(cmd_list),collapse = " & ")
shell(cmd)
}
My gitstatus, gitadd, and gitpush functions work. The gitcommit function does not work. It generates the following error:
fatal: Paths with -a does not make sense.
Warning message:
In shell(cmd) : 'd: & cd D:/Documents/R/my_path & git commit -am 'commit from Rstudio'' execution failed with error code 128
The gitpush function works because if you switch to the terminal or git within Rstudio, you can commit changes and then successfully call gitpush.
Any ideas on how to fix this issue?
...
Note: I have Git bash installed, and I can successfully use git from the Windows command terminal and Rstudio. I also tried an alternative strategy which was to have R write a temporary .bat file and then execute this, but this strategy also gets hung up on the commit step.
Solution
The answer lie within Dirk Eddelbuettel's drat package function addrepo. It was also necessary to use git2r's config function to insure that git recognizes R. git2r's functions probably provide a more robust solution for working with git from an R script in the future. In the meantime, here's how I fixed the problem.
Install git2r. Use git2r::config() to insure git recognizes R.
From Dirk's code I modified the gitcommit() function to utilize sprintf() and system() to execute a system command:
# Git commit.
gitcommit <- function(msg = "commit from Rstudio", dir = getwd()){
cmd = sprintf("git commit -m\"%s\"",msg)
system(cmd)
}
Sprintf's output looks like this:
[1] "git commit -m\"commit from Rstudio\""
Example
#install.packages("git2r")
library(git2r)
# Insure you have navigated to a directory with a git repo.
dir <- "mypath"
setwd(dir)
# Configure git.
git2r::config(user.name = "myusername",user.email = "myemail")
# Check git status.
gitstatus()
# Download a file.
url <- "https://i.kym-cdn.com/entries/icons/original/000/002/232/bullet_cat.jpg"
destfile <- "bullet_cat.jpg"
download.file(url,destfile)
# Add and commit changes.
gitadd()
gitcommit()
# Push changes to github.
gitpush()
Well, the pic looks wonky, but I think you get the point.
From what I have read in this Ask Ubuntu question, you should be using &&, not &, to separate multiple commands in the Git bash. Try doing that:
gitcommit <- function(msg = "commit from Rstudio", dir = getwd()) {
cmd_list <- list(
cmd1 = tolower(substr(dir, 1, 2)),
cmd2 = paste("cd", dir),
cmd3 = paste0("git commit -am ", "'", msg, "'")
)
cmd <- paste(unlist(cmd_list),collapse = " && ")
shell(cmd)
}
Note that your gitcommit function will output something like this:
/v & cd /var/www/service/usercode/255741827 & git commit -am 'first commit'"
I don't know what purpose the substr(dir, 1, 2) portion serves, but if my suggestion still doesn't work, then try removing it to leave just the cd and git commit commands.
I personally like the syntax and simplicity of the gert package from rOpenSci. Specifically, to commit a repo you would do:
git_add("test.txt")
git_commit("Adding a file", author = "jerry <jerry#gmail.com>")
Top push to remote:
git_push(remote = "origin", repo = ".")
And see all other useful functions with this simple syntax.

Using GitBash Add - Commit - Push file to a GIT repository from WebApplication on Button Click

I have a requirement where I have to Add - Commit - Push 3 newly created Files into our Git Repository using GitBash commands on Button Click in a Web Application.
I have tried the following but in vain:
{
string gitCommand = "git bash";
string gitAddArgument = #"add -A" ;
string gitCommitArgument = #"commit""explanations_of_changes"" ";
string gitPushArgument = #"push our_remote";
Process.Start(gitCommand, gitAddArgument );
Process.Start(gitCommand, gitCommitArgument );
Process.Start(gitCommand, gitPushArgument );
}
On Button Click I need to Call the Git Bash Commands, Commit the 3 Files and then Push to Master Origin.
You have a few errors within your Git commands..
To add all files use: git add -A Note the capital A
To commit all modified files with a message use: git commit -a -m "Your message here"
To push your changes to remote use git push
To make your C# code complete:
{
string gitCommand = "git";
string gitAddArgument = "add -A" ;
string commitMessage = "Your commit message";
string gitCommitArgument = String.Format("commit -a -m \"{0}\"", commitMessage);
string gitPushArgument = "push";
Process.Start(gitCommand, gitAddArgument); // Runs: git add -A
Process.Start(gitCommand, gitCommitArgument); // Runs: git commit -a -m "Your commit message"
Process.Start(gitCommand, gitPushArgument); // Runs: git push
}
Note
Make sure the program git is within your PATH variabele else you wont be able to use it this way. If it's not in your PATH, use the absolute path to the git executeable
For example on linux it would be: /usr/bin/git this should be the text for gitCommand

QT5.8. custom Virtual keyboard including

I'm writing a custom virtual keyboard, which based on QtVirtualKeyboard. For my project I would need to be able to use my version of keyboard.
But the only method, which i found is recompile project and replace original "qtvirtualkeyboardplugin.dll" in "mingw53_32\plugins\platforminputcontexts" on my version of "qtvirtualkeyboardplugin.dll". And use the qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard")); function in main.cpp
After hours of reading the docs and trying various things I'm still cannot find method to use custom keyboard localy, without deleting original "qtvirtualkeyboardplugin.dll".
I'm assuming that you've forked the code, in which case the following modifications seem to be enough to get a differently named plugin to be installed to plugins/platforminputcontexts:
Rename qtvirtualkeyboard/src/virtualkeyboard/qtvirtualkeyboard.json to qtvirtualkeyboard/src/virtualkeyboard/customvirtualkeyboard.json.
In customvirtualkeyboard.json, rename the qtvirtualkeyboard key to customvirtualkeyboard.
In qtvirtualkeyboard/src/virtualkeyboard/plugin.cpp, change the contents of the pluginName string to customvirtualkeyboard.
In qtvirtualkeyboard/src/virtualkeyboard/plugin.h, change the FILE string to customvirtualkeyboard.
In src/virtualkeyboard/virtualkeyboard.pro, change TARGET = qtvirtualkeyboardplugin to TARGET = customvirtualkeyboardplugin. This affects the name of the installed .dll, .lib, etc. that you see in plugins/platforminputcontexts.
Here are the changes as a Git diff:
diff --git a/src/virtualkeyboard/customvirtualkeyboard.json b/src/virtualkeyboard/customvirtualkeyboard.json
new file mode 100644
index 0000000..9ef7a87
--- /dev/null
+++ b/src/virtualkeyboard/customvirtualkeyboard.json
## -0,0 +1,3 ##
+{
+ "Keys": [ "customvirtualkeyboard" ]
+}
diff --git a/src/virtualkeyboard/plugin.cpp b/src/virtualkeyboard/plugin.cpp
index 73ddeab..4abe9a4 100644
--- a/src/virtualkeyboard/plugin.cpp
+++ b/src/virtualkeyboard/plugin.cpp
## -76,7 +76,7 ## using namespace QtVirtualKeyboard;
Q_LOGGING_CATEGORY(qlcVirtualKeyboard, "qt.virtualkeyboard")
-static const char pluginName[] = "qtvirtualkeyboard";
+static const char pluginName[] = "customvirtualkeyboard";
static const char inputMethodEnvVarName[] = "QT_IM_MODULE";
static const char pluginUri[] = "QtQuick.VirtualKeyboard";
static const char pluginSettingsUri[] = "QtQuick.VirtualKeyboard.Settings";
diff --git a/src/virtualkeyboard/plugin.h b/src/virtualkeyboard/plugin.h
index 08074d1..19593a4 100644
--- a/src/virtualkeyboard/plugin.h
+++ b/src/virtualkeyboard/plugin.h
## -38,7 +38,7 ##
class QVirtualKeyboardPlugin : public QPlatformInputContextPlugin
{
Q_OBJECT
- Q_PLUGIN_METADATA(IID QPlatformInputContextFactoryInterface_iid FILE "qtvirtualkeyboard.json")
+ Q_PLUGIN_METADATA(IID QPlatformInputContextFactoryInterface_iid FILE "customvirtualkeyboard.json")
public:
QStringList keys() const;
diff --git a/src/virtualkeyboard/qtvirtualkeyboard.json b/src/virtualkeyboard/qtvirtualkeyboard.json
deleted file mode 100644
index 76d1706..0000000
--- a/src/virtualkeyboard/qtvirtualkeyboard.json
+++ /dev/null
## -1,3 +0,0 ##
-{
- "Keys": [ "qtvirtualkeyboard" ]
-}
diff --git a/src/virtualkeyboard/virtualkeyboard.pro b/src/virtualkeyboard/virtualkeyboard.pro
index 4f3ca69..e9b0ff9 100644
--- a/src/virtualkeyboard/virtualkeyboard.pro
+++ b/src/virtualkeyboard/virtualkeyboard.pro
## -1,4 +1,4 ##
-TARGET = qtvirtualkeyboardplugin
+TARGET = customvirtualkeyboardplugin
DATAPATH = $$[QT_INSTALL_DATA]/qtvirtualkeyboard
QMAKE_DOCS = $$PWD/doc/qtvirtualkeyboard.qdocconf
Remember that if you're using an open source license, you have to make modifications to Qt code available to users of your application.

Resources