How to retrieve SHA1 of a file from the ObjectDatabase - libgit2sharp

I've checked the source code but couldn't find a method to retrieve SHA1 of a file from the ObjectDatabase by its path.
It's easy to calculate SHA1 manually but it requires reading a file which might cause performance issues.

If your file has been previously committed:
Commit commit = repo.Lookup<Commit>(commitSha);
// Or if it's just been committed
// Commit commit = repo.head.Tip;
string shaFromKnownCommit = commit["path/to/file.txt"].Target.Id.Sha;
If your file has only been staged (promoted to the staging area):
string shaFromStagedFile = repo.Index["path/to/file.txt"].Id.Sha;

Related

Trying to rename a file in Amazon S3. Getting specified key does not exist error

I followed the documentation here
This is how I'm trying to rename my file:
bucket = Aws::S3::Bucket.new("bucket-key")
object = S3.bucket.object("key-to-object")
object.move_to(bucket: "bucket-key", key: "key-to-destination")
I've also tried changing the target to a string and an object but both don't work :(
I've also tried just using the copy_to and delete commands but the copy_to is erroring just the same.

Read rule argument value from config file

Consider the following bazel rule written in a WORKSPACE file:
container_pull(
name = "release-base",
registry = "mydockernet:9443",
repository = "release-base",
digest = "sha256:...",
tag = "1.8.2",
)
The problem is that the tag value 1.8.2 is written in a yaml config file and we want to respect the DRY principle (read the value from the config file instead of duplicating the value in bazel files). Is there a way to handle this?
It's not yaml but you can define things in another bzl file and then load them into your WORKSPACE:
load("common.bzl", "MYVERSION")
container_pull(
name = "release-base",
registry = "mydockernet:9443",
repository = "release-base",
digest = "sha256:...",
tag = MYVERSION,
)
then in common.bzl:
MYVERSION=1.8.2

read checksum from artifactory given the artifact url

I want to read checksum from artifactory given the artifactory url and append it to an attribute.
I tried to look for examples but people are hardcoding the checksum value like below.
If I hardcode the value, I will have to update it when I have new artifact. I do not want to do that.
Please let me know if there is anyway to get this value from artifactory.
I have a code to compute checksum in the my chef code using digest. I will compare the checksum from artifactory and the checksum I computed in the recipe.
source 'http://www.example.com/tempfiles/testfile'
mode '0755'
checksum '3a7dac00b1' # A SHA256 (or portion thereof) of the file.
end
To compare the computed checksum with the local checksum, I have seen people hardcoding local checksum value. Instead I want to read it from artifactory through chef. ex:
```computed_checksum = Digest::SHA2.file(temp.path).hexdigest Artifactory_checksum = Read from artifactory ?
if Artifactory_checksum != computed_chceksum throws error.....''''
require 'open-uri'
require 'tempfile'
jar_file = 'https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar'
temp = Tempfile.new
temp << open(jar_file).read
temp.flush
actual_sha1 = Digest::SHA1.file(temp.path).hexdigest
sha1_file = 'https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.17/mysql-connector-java-8.0.17.jar.sha1'
expected_sha1 = open(sha1_file).read.strip
p actual_sha1 == expected_sha1

Using Grails to store image but could not store outside CATALINA_HOME in production

I'm using Grails 2.5.6 to store uploaded images to folder on a server.
The following are my code to store the image
mpr.multiFileMap.file.each{fileData->
CommonsMultipartFile file = fileData
File convFile = new File(file.getOriginalFilename());
file.transferTo(convFile);
/** Processing File **/
File uploadedFile = new File("${directory}${generatedFileName}.${extension}")
convFile.renameTo(uploadedFile)
}
I have no problem running on development (MacOSX High Sierra)
But when i deployed on production (Ubuntu 14.04 server), i could not save the file outside CATALINA_HOME directory.
I have checked the permission and ownership of the destination directory, but still, the directory was created but the file was never stored.
For Example, i've tried to store the file on /home/tomcat/ directory (/home directory was in separate partition with tomcat which stored it /var), the directory was created, but the file was never stored.
When i put the destination directory within CATALINA_HOME folder, everything works fine. But this was not the scenario i want to do.
You say your destination directory is on another partition, so maybe another filesystem is used on this partition.
Or if you look on the javadoc of the renameTo method it is said :
Many aspects of the behavior of this method are inherently
platform-dependent: The rename operation might not be able to move a
file from one filesystem to another, it might not be atomic, and it
might not succeed if a file with the destination abstract pathname
already exists. The return value should always be checked to make
sure that the rename operation was successful.
...
#return true if and only if the renaming succeeded;
false otherwise
Thus I think the renameTo method is not able to move the file, don't know why but you can rewrite your code like this :
mpr.multiFileMap.file.each{fileData->
CommonsMultipartFile file = fileData
File uploadedFile = new File("${directory}${generatedFileName}.${extension}")
// String originalFilename = file.getOriginalFilename()
// you can store originalFilename in database for example
if(!uploadedFile.getParentFile().exists()) {
uploadedFile.getParentFile().mkdirs()
// You can set permissions on the target directory if you desired, using PosixFilePermission
}
file.transferTo(uploadedFile)
}

How to `nix-build` again a built store path?

I create my own repository to fetch some git source.
# packages.nix
with (import <nixpkgs> {});
rec {
rustcSource = fetchgit {
url = https://github.com/rust-lang/rust;
rev = "3191fbae9da539442351f883bdabcad0d72efcb6";
sha256 = "0w1l14kz9kxyj5dw3w9xxk1fzww5xqs3sf8saay0mh7nkmvrdb59";
};
}
Then I build rustcSource,
sudo nix-env -f package.nix -A rustcSource
It reveals a store path of /nix/store/096fpy9qjbz5r14aadjnq9d2md9ql9cg-rust-3191fba. The problem is, I forgot to download it's submodules, so I change my expression to include submodules,
with (import <nixpkgs> {});
rec {
rustcSource = fetchgit {
url = https://github.com/rust-lang/rust;
rev = "3191fbae9da539442351f883bdabcad0d72efcb6";
sha256 = "0w1l14kz9kxyj5dw3w9xxk1fzww5xqs3sf8saay0mh7nkmvrdb59";
leaveDotGit = true;
fetchSubmodules = true;
};
}
But, I discovered that nix-build doesn't recalculate the hash and see that the path has been built. So, it ends up not downloading the submodules.
Q: Is it nix bug? How can I "rebuild" or redownloading the repository?
P.S. Actually I create a derivation with fetchgit in it, but it fails because the submodules doesn't being downloaded. So, the above case simplify my problem.
Not a bug, this is by design.
Derivations that specify a hash are called fixed-output derivations and they only check if hash matches any existing derivations in store and use that. So your fetchSubmodules change is ignored.
For more discussion see https://github.com/NixOS/nix/issues/969
To fix the issue, you need to change the hash to some value that isn't already a valid hash of any path in your nix store.
For fixed-output derivations (those are the ones that have an explicit hash specified and only those get network access), if the hash already matches a path in the nix store, then nix will skip the download and just use the existing path. So slightly changing the hash (so that it no longer matches) should be enough to force a rebuild.
Fetching submodules will result in a package with a different hash. The easiest way to fix this is to change the hash to an invalid value and rebuild the package. The error message will include the correct hash. Use that and rebuild.

Resources